我用什么路线来引用当前用户个人资料的路径?设计

时间:2013-11-30 20:00:30

标签: ruby-on-rails ruby rest devise routes

我的应用程序的用户在创建个人资料时上传了头像。以下是我在导航栏中显示其个人资料图片的小版本的方法:

 <li><%= link_to image_tag current_user.avatar(:nav) %></li>

我希望该链接转到当前用户的个人资料页面。这是我的路线:

  Prefix Verb   URI Pattern                    Controller#Action
                  things GET    /things(.:format)              things#index
                         POST   /things(.:format)              things#create
               new_thing GET    /things/new(.:format)          things#new
              edit_thing GET    /things/:id/edit(.:format)     things#edit
                   thing GET    /things/:id(.:format)          things#show
                         PATCH  /things/:id(.:format)          things#update
                         PUT    /things/:id(.:format)          things#update
                         DELETE /things/:id(.:format)          things#destroy
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                   users POST   /users(.:format)               users#create
                new_user GET    /users/new(.:format)           users#new
                    user GET    /users/:id(.:format)           users#show
                   about GET    /about(.:format)               pagess#about
                    root GET    /                              things#index

我目前正在使用Devise进行身份验证。

这是我的UsersController

class UsersController < ApplicationController
  def show
    @user = User.find_by_username(params[:id])
  end

  def user_params
      params.require(:user).permit(:avatar)
  end
end

2 个答案:

答案 0 :(得分:2)

根据您提供的路线,您的用户个人资料为users#show。所以你要寻找的路径应该是user_path

#app/controllers/users_controller.rb
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id]
    ...
  end
end

然后在app/views/users/show.html.erb中显示用户的个人资料信息。

答案 1 :(得分:1)

尝试:

<li><%= link_to (image_tag current_user.avatar(:nav)), user_path(current_user.id) %></li>

你也可以只使用'current_user'而不是'current_user.id'