我正在使用设计来验证用户和管理员(admin:true)。作为管理员,我想访问用户个人资料的页面,但我总是到达我自己的个人资料(作为current_user)。我不知道怎么做......
用户也可以看到其他用户个人资料
感谢您的帮助
用户/ index.html.slim
.container
h1 All the users
.row
table.board
thead
tr
th First Name
th Last Name
th Email Address
th Action on User
hr
tbody.board
-@users.each do |user|
.row
.col-xs-3
= user.first_name
.col-xs-3
= user.last_name
.col-xs-3
= user.email
.col-xs-1
#The problem is this link
= link_to 'View', user_path(user.id), class:'btn btn-success'
.col-xs-1
= link_to 'Remove', user_path(user), class:'btn btn-danger', method: :delete, data: {confirm: "Are you sure?"}
hr
users_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!
def show
#binding.pry
#@user = User.find(current_user)
#@user.id = User.find(params[:id])
@user = User.find(user_params[:id]) || current_user
@tutos= Tuto.all
end
def index
if current_user.admin == true
@users = User.all
else
redirect_to root_path
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
flash[:success] = "User was successfully deleted"
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :id)
end
end
路线 #edited Rails.application.routes.draw做
namespace :users do
resources :tutos
end
resources :tutos, only: [:show]
resources :tutos do
member do
put "like", to: "tutos#upvote"
end
end
get "/register", to: "devise/registrations#new", as: :register
get "/login", to: "devise/sessions#new", as: :login
get "/logout", to: "devise/sessions#destroy", as: :logout
get "/account", to: "users#show", as: :account
get "/login" , to: "devise/sessions#new", as: :new_user_session
post "/login" , to: "devise/sessions#create", as: :user_session
delete "/logout" , to: "devise/sessions#destroy", as: :destroy_user_session
devise_for :users, skip: [:sessions]
resources :users
root "home#landing"
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
users_tutos GET /users/tutos(.:format) users/tutos#index
POST /users/tutos(.:format) users/tutos#create
new_users_tuto GET /users/tutos/new(.:format) users/tutos#new
edit_users_tuto GET /users/tutos/:id/edit(.:format) users/tutos#edit
users_tuto GET /users/tutos/:id(.:format) users/tutos#show
PATCH /users/tutos/:id(.:format) users/tutos#update
PUT /users/tutos/:id(.:format) users/tutos#update
DELETE /users/tutos/:id(.:format) users/tutos#destroy
like_tuto PUT /tutos/:id/like(.:format) tutos#upvote
tutos GET /tutos(.:format) tutos#index
POST /tutos(.:format) tutos#create
new_tuto GET /tutos/new(.:format) tutos#new
edit_tuto GET /tutos/:id/edit(.:format) tutos#edit
tuto GET /tutos/:id(.:format) tutos#show
PATCH /tutos/:id(.:format) tutos#update
PUT /tutos/:id(.:format) tutos#update
DELETE /tutos/:id(.:format) tutos#destroy
register GET /register(.:format) devise/registrations#new
login GET /login(.:format) devise/sessions#new
logout GET /logout(.: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
account GET /account(.:format) users#show
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
GET /tutos(.:format) tutos#index
POST /tutos(.:format) tutos#create
GET /tutos/new(.:format) tutos#new
GET /tutos/:id/edit(.:format) tutos#edit
GET /tutos/:id(.:format) tutos#show
PATCH /tutos/:id(.:format) tutos#update
PUT /tutos/:id(.:format) tutos#update
DELETE /tutos/:id(.:format) tutos#destroy
root GET / home#landing
在最后一次编辑后我还有问题.... 当我尝试继续使用account_path时出现此错误
只是提醒你,我正在使用设计:
当用户登录时,如果我想查看自己的个人资料,我会使用
account_path
(这项工作做得很好)
访问用户个人资料页面的链接如下所示:
= link_to 'View', user_path(user)
但它看起来完全像:account_path
。 (所以在我的个人资料中,而不是我想访问的用户)
如果我使用@user = User.find(user_params[:id]) || current_user
或 #@user = User.find(user_params[:id])
我有以下错误:
ActionController::ParameterMissing in UsersController#show
param is missing or the value is empty: user
如果我使用@user = User.find(current_user)
。
每次我都会在我自己的个人资料中重定向....
def show
#binding.pry
#@user = User.find(current_user)
#@user = User.find(user_params[:id])
@user = User.find(user_params[:id]) || current_user
@tutos = Tuto.all
end
答案 0 :(得分:1)
这会将@user设置为当前用户或管理员
的请求用户@user = current_user.admin? ? User.find(params[:id]) : current_user
答案 1 :(得分:0)
在#show
中你可以这样:
# Assuming params[:id] is the ID of the user's profile you're trying to view
def show
user_id = current_user.admin? ? params[:id] : current_user.id
@user = User.find(user_id)
end
答案 2 :(得分:0)
问题在于您的路径文件已删除&#39;用户做&#39;阻止它会产生一些奇怪的东西:
user GET /users/:id(.:format) users#show
GET /tutos/new(.:format) tutos#new
GET /tutos/:id/edit(.:format) tutos#edit
GET /tutos/:id(.:format) tutos#show
答案 3 :(得分:0)
是否要使用account_path
查看其他用户的个人资料,或者您可以/users/2
使用user
的{{1}}转到id
吗?它使用控制器中的show
操作,并使用相关的user
路由到id
路径?