colmtuite
colmtuite

Reputation: 4491

Rails 4 user routing

<%= link_to "Whatever", current_user %>

is linking to /user.id

My routes are setup like this

resource :user, except: [:index, :destroy]

So it should be linking to /user, right?

When I visit /user it says "Couldn't find User without an ID".

My user show action looks like this

def show
    @user = User.find(params[:id])
end

Upvotes: 0

Views: 45

Answers (2)

Kirti Thorat
Kirti Thorat

Reputation: 53018

The reason you are getting /user.id is because you have defined your routes as

resource :users, except: [:index, :destroy]

Note the singular resource which will create all the routes without any :id. As you don't have an accepting param within the route, the current_user that you are passing is matched to the format i.e., like .html, .js, etc. which in your case becomes .id

I would recommend using resources (note the plural)

resources :users, except: [:index, :destroy]

This will resolve the error Couldn't find User without an ID as you would be passing an params id within your route.

NOTE:

As per Rails convention, controller name should be plural. For UsersController, resources should be resources :users

Upvotes: 2

rafroehlich2
rafroehlich2

Reputation: 1358

I have had this happen many times. My fix is to use the path helpers.

<% link_to "Whatever", user_path current_user %>

This will drop the .id and make it /user/id

Upvotes: 0

Related Questions