2014-02-13 108 views
0

真正的代码:将json发送给控制器时NoMethodError'title_url'?

class TitlesController < ApplicationController 
    respond_to :json 
    def create 
    title = Title.create!(params[:title]) 
    respond_with title 
    end 
end 

规格:

describe TitlesController do 
    it 'receives a json response' do 
    post :create, title: { name: 'fancy title'}, format: 'json' 
    expect(controller).to respond_with(201) 
    end 
end 

==> NoMethodError:对#

未定义的方法`title_url”世界为什么会这样想打电话title_url? ?????

+0

找出您是否在代码库中的某处使用了'title_url'。运行'grep -rin“title_url”*' –

+0

@patrick,我不确定。但请尝试在测试中将格式作为符号传递。还要确保格式是作为'json'传递的 – usha

回答

1

当您使用资源回复时,在这种情况下,title对于发布请求,它将重定向到show操作。从respond_with documentation

If there are no errors, i.e. the resource was saved successfully, the response redirect's to the resource i.e. its show action.

要获取show动作的URL,它会调用title_url(title)

您可以通过指定一个:location(涵盖了文档的定制响应特性部分)覆盖这个重定向行为:

respond_with title, location: some_other_url 
+2

@Shadewell:它不是只在'html'请求的情况下重定向到'show'吗? – usha

+0

你这个男人..但该死的,那是dumbo – patrick

+0

嗯,实际上,不是男人,@Vimsha是对的。该行为仅适用于'html'请求。对于json请求,它应该只呈现标题的json表示。 – Shadwell

相关问题