我有一个Rails 4.2.beta1应用程序,我试图在成功创建一个新对象后返回:show
方法。资源嵌套在以下方法中:
resources :accounts do
resources :lists do
end
我得到的错误是:
ActionController::UrlGenerationError (No route matches {
:account_id=>#<List id: 21, creator_id: 1, list_type_id: 1,
name: "Test 16", description: nil,
created_at: "2014-10-07 03:59:56",
updated_at: "2014-10-07 03:59:56">,
:action=>"show", :controller=>"lists", :format=>nil, :id=>nil}
missing required keys: [:id]):
app/views/lists/show.json.jbuilder:2:in `_app_views_lists_show_json_jbuilder__4188070110312340824_70141816016640'
app/controllers/lists_controller.rb:31:in `create'
我在这里看到的是,rails错误地认为新的List
对象是account_id
...
以下是我的create
操作:
# POST /lists
# POST /lists.json
def create
@list = List.new(creator: @account, list_type: ListType.find(list_params[:list_type_id]), name: list_params[:name])
if @list.save
@account.subscriptions << @list
render action: :show, status: :created, location: [@account, @list] and return
end
render json: @list.errors, status: :unprocessable_entity
end
我找到了这个Q / A here并且在那里得到了所有答案而结果没有变化。为了列出它们,我尝试了以下所有......
#1
render action: :show, status: :created, location: [@account, @list] and return
#2
render action: 'show', status: :created, location: [@account, @list] and return
#3
render action: :show, status: :created, location: [@list.creator, @list] and return
#4
render action: :show, status: :created, location: @list and return
#5
render action: :show, status: :created, location: [@list, @account] and return
#6
render action: :show, status: :created, location: [:account, @list] and return
#7
render action: :show, status: :created, location: accounts_list_url(@account, @list) and return
#8
render action: :show, status: :created and return
#9
redirect_to action: :show, status: :created, location: [@account, @list] and return
#10
redirect_to accounts_lists_url(@account, @list) and return
所有这些都产生相同的错误(或明显的错误,比如像#5那样使用错误的顺序)
需要注意的另一件事是#7&amp;&amp; #10产生错误:
NoMethodError (undefined method `accounts_lists_url' for #<ListsController:0x007f9652235c40>)
对我来说这似乎也很奇怪
答案 0 :(得分:1)
所以....我觉得自己像个白痴。我正在阅读错误消息,但实际上并没有 正在阅读 错误消息。
问题发生在我的show.json.jbuilder
文件中。我有account_list_url(@list)
电话,这显然是不正确的。我以为我在那里检查了但显然没有。
带回家消息:如果错误消息显示带有jbuilder的堆栈跟踪。查看jbuilder文件...