【问题标题】:Issues with ActionView::MissingTemplate: Missing templateActionView::MissingTemplate 的问题:缺少模板
【发布时间】:2013-09-27 19:17:29
【问题描述】:

我正在使用带有rails 的Ajax 调用来调用我的控制器中的方法。我的问题是代码本身工作正常,我看不到任何错误。当我去测试代码时,我得到了

ActionView::MissingTemplate: Missing template branch/call_s ml], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "C:/home/Ruby/rails/showrooms/texting/app/views" * "C:/home/Ruby/rails/showrooms/texting/app/views/shared" * "C:/home/Ruby/rails/showrooms/texting/app/views"

这是我的控制器方法。

  # Sends a SMS message to selected phone number. 
  # 
  def call_sms
    if current_associate    
      @text_number    = params[:phone_number]
      @text_message   = params[:text_message].to_s
      if !@text_number.empty? && !@text_message.empty?

        @sms = ShortMessagingService.new  
        if @sms.send(@text_number, @text_message)       
          @sms_message = "Status: #{@sms.sent?}"
        end
      else
       @sms_message = 'Phone number and text field cannot be blank.'
     end
   else
     @sms_message = 'You do not have perission for this feature.'
   end
 end

这是路线

# Small Messaging Service rout. 
post 'call_sms' => 'branch#call_sms'
match 'call_sms' => 'branch#call_sms'

这是表格:

<%= form_for :call_sms, url: {action: "call_sms"},  
    :method => :post, :remote => true, 
    :html => {:id => 'sms_form'} do |f| 
%> 
.
.
.
<% end %>

这是我的测试

  test "Can send sms messages." do
    phone_number  = '18006388875'
    text_message  = "Hey this is a test. Don't text and drive."
    post :call_sms
    assert_response :ok
  end

【问题讨论】:

    标签: ruby-on-rails unit-testing jquery templates


    【解决方案1】:

    您的表单正在执行 ajax 请求,但您的测试用例没有。

    不要使用post :call_sms,试试这个:

    xhr :post, :call_sms
    

    这告诉测试用例发送 ajax 帖子而不是常规帖子。

    【讨论】:

    • 天才!需要几天来寻找这个。该死的互联网。
    • 如果我添加 render :nothing =&gt; true 它不会加载 call_sms.js.erb 并且我在控制台中收到错误
    • 啊,好吧,我没有意识到你确实有一个 js.erb 文件。在这种情况下,只需将测试用例中的post :call_sms 替换为xhr :post, :call_sms
    猜你喜欢
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多