我有一个 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并遵循了那里的所有答案,结果没有变化。为了列出它们,我尝试了以下所有方法......
#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 中的 id 顺序错误)
另一件需要注意的是 #7 && #10 产生错误:
NoMethodError (undefined method `accounts_lists_url' for #<ListsController:0x007f9652235c40>)
这对我来说也很奇怪