开发者

How do you navigate to a users show page with devise?

开发者 https://www.devze.com 2023-03-20 02:14 出处:网络
Like the title says, I am using devise. I want to navigate to /users/1 or whatever the current_users path is. When I try link_to(current_user.name, user_path) I just get an error.

Like the title says, I am using devise. I want to navigate to /users/1 or whatever the current_users path is. When I try link_to(current_user.name, user_path) I just get an error. undefined local variable or method `user' for #<#:0x00000101d2dfc8>

Am 开发者_JAVA技巧I not using the right path? Do I need to route it or something?

Also, this goes with the first, how would i navigate to a different users show page(not the current user)


You will need to to three things to get the view you want because devise doesn't provide the standard user routes.

1) Create a route in config/routes.rb. Devise doesn't provide a standard route, and in my experience rake routes just confirms this. I've used:

match '/users/:id', :to => 'users#show',    :as => :user,         :via => :get

with mixed success

2) Create a user controller with the show section appropriately filled out

3) create a view file: user/show and add the parts you want to show.


I am not familiar with devise, but I'd be surprised if devise differed from Rails conventions. You need to pass in the user to the route helper: link_to(current_user.name, user_path(current_user))


What helped for me was to add the @ before user in "routes.rb":

"match 'users/:id' => 'users#show', :as => @user"
0

精彩评论

暂无评论...
验证码 换一张
取 消