我是 Rails 的新手,我似乎遇到过类似的问题,但我无法解决我的问题。
我的路线:
resources :users do
resources :items
end
我的模型:
class Item < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :items
end
HTML:
<% @items.each do |item| %>
<tr>
<td><%= item.id %></td>
<td><%= item.code %></td>
<td><%= item.name %></td>
<td><%= item.quantity %></td>
<td><%= link_to "Edit", edit_user_item_path(item) %></td> <---- error
我遇到了同样的错误:
No route matches {:action=>"edit", :controller=>"items",
:user_id=>#<Item id: 1, user_id: 1, code: "123", name: "test",
quantity: 12, , created_at: "2014-02-11 15:45:30", updated_at:
"2014-02-11 15:45:30">, :id=>nil, :format=>nil} missing required keys: [:id]
最佳答案
您还需要包括用户,因为它是一个嵌套路由。所以像这样:
<td><%= link_to "Edit", edit_user_item_path(@user, item) %></td>
关于html - 没有路由匹配缺少所需的键 : [:id],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21708256/