53

I'm new at Rails and I've seem similar problems, but I can't solve mine.

My routes:

resources :users do
    resources :items
end

My models:

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

And I'm getting the same 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]
atw
  • 5,428
  • 10
  • 39
  • 63
letz
  • 1,762
  • 1
  • 20
  • 40

5 Answers5

70

You need to include the user as well since its a nested route. So something like:

<td><%= link_to "Edit", edit_user_item_path(@user, item) %></td>
jklina
  • 3,407
  • 27
  • 42
  • 1
    shouldn't this be: edit_user_item_path(item.user, item)? – Hesham Feb 11 '14 at 17:11
  • 1
    You could do that, but you usually use nested routes because you want to assign the `@user` variable to be used in the view. If you're not using `@user` then you might not need a nested route. – jklina Feb 11 '14 at 17:15
  • Can you tell me for the case that i use form_for ? what should i put? – letz Feb 11 '14 at 18:52
  • `<%= form_for([@user, @item]) do |f| %> ... <% end %>` Docs are here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for – jklina Feb 11 '14 at 19:00
  • In a similar instance of this for me in rails 4, I had to say `edit_parent_child_path(params[:parent_id], child)` In case that helps anyone else. – kingsfoil Aug 11 '14 at 17:14
10

The problem is that you are using nested resources:

resources :users do
   resources :items
end

So when you have a link:

<%= link_to "Edit", edit_user_item_path(item) %> 

It will lack one user_id so the easy to check the issue is using rake routes. And it will list the routes like this:

edit_user_item GET    /users/:user_id/items/:id/edit(.:format) items#edit

You can see the routes above and check it with the link, you will see it does not have user_id. That's the main reason!

Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43
Hai Nguyen
  • 458
  • 9
  • 15
5

You've missed user_id in the following path:

edit_user_item_path(user_id, item)

format you are able to find just running bundle exec rake routes | grep edit_user_item

itsnikolay
  • 17,415
  • 4
  • 65
  • 64
  • I had NEVER consider piping the output of `bundle exec rake routes` to grep. Brilliant. – Tass Jul 05 '16 at 15:27
4

The object item is being passed instead of the required id.

<td><%= link_to "Edit", edit_user_item_path(item.id) %></td>
Zero Fiber
  • 4,417
  • 2
  • 23
  • 34
-1

<%= link_to "Edit", edit_user_item_path(@user, item) %>

and in item form <%= form_for([@user, @item]) do |f| %> ... <% end %>

Ican Doit
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 28 '22 at 08:56