Domanda

I'm trying to add a show page to my devise users but can't seem to get the id value passed properly, how do I create a link to the show page? For some reason rails is doing this to URL /show.1

users controller:

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

user/show.html.erb

<h1><%= @user.username %></h1>

link to user profile also gives me issues id no method

<%= link_to @post.email, users_show_path(@user.id) %>

routes.rb

 get "users/show"
 get "pages/faq"
 get "pages/about"
 devise_for :users
 resources :posts
È stato utile?

Soluzione

You have defined the show route as:

get "users/show"

Notice that there is no dynamic segment in this route, like :id. That means your route is not expecting any value to be passed. BUT while linking to this route you passed @user.id

<%= link_to @post.email, users_show_path(@user.id) %>

which the route was not expecting. So, Rails assumed that you are passing the format (like .html, .json, etc.) which is why you see the url formed as users/show.1.

To fix this, I would suggest you to add a dynamic segment to your route to capture the id of a user correctly.

Change

 get "users/show"

With

 get "users/show/:id" => "users#show", as: :users_show

For a user with id = 1, when you click on the user profile link the url generated would be http://yourdomain/users/show/1

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top