2013-09-27 49 views
0

我正在使用Rails的Ajax调用来调用我的控制器中的方法。我的问题是,代码本身工作正常,我没有看到任何错误。当我去测试代码,我得到ActionView :: MissingTemplate的问题:缺少模板

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 [email protected]_number.empty? && [email protected]_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 

回答

1

您的形式进行处理Ajax请求,但你的测试用例不是。

相反的post :call_sms,试试这个:

xhr :post, :call_sms 

这告诉测试用例发送一个ajax后,而不是常规的职位。

+0

天才!需要寻找这个好几天。该死的interwebs。 –

+0

如果我添加'render:nothing => true'它不会加载call_sms.js.erb并且我的控制台出现错误 –

+0

啊,好吧,我没有意识到你有一个js.erb文件。在这种情况下,只需用'xhr:post,:call_sms'替换测试用例中的'post:call_sms'。 –

相关问题