Rails Dead End – No Route Matches

There’s a question that comes up frequently by new Rails developers and I thought it is a good idea to fully describe it once and for all.

My routes.rb file is as follows:

resources :users

And I want to put a link to edit user page in one of my views:

<%= link_to "Edit", edit_user_page %>

But when I try to render the view I get the following error:

No route matches {:action=>"edit", :controller=>"users"}

Looks strange? Let’s dig inside a little bit and figure this out:
First step is running rake routes to verify that our routes.rb files maps correctly to url helpers and that we’re using an existing one. The answer for rake routes should include this line:

edit_user GET    /users/:id/edit(.:format) users#edit

If it doesn’t then probably you’ve defined other routes which override the definition of resources :users in routes.rb

The important thing to pay attention to here is that edit_user_path url helper requires an ID. You can tell that by identifying the :id section in the generated url. If so, edit_user_path cannot be used by itself, it must be passed either an ID (number) or an object that responds to the id method.

The correct form to use the url helper is edit_user_path(@user) or edit_user_path(some_user_id).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s