[英]Allowing users to edit some information only on ruby on rails but not admins
我正在開發一個 Rails 應用程序,我希望它只允許用戶編輯他們的一些信息,例如姓名或個人信息,但我希望管理員用戶能夠編輯所有內容。 有沒有人幫我解決這個問題?
感謝@tadman,這就是我想出的
def user_params_UserEdit
if current_user?(@user)
params.require(:user).permit( :password,
:password_confirmation)
elsif current_user.admin?
params.require(:user).permit(:name, :email, :password,
:password_confirmation, :admin, :personel_number)
end
end
和更新:
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params_UserEdit)
flash[:success] = "Profile updated"
redirect_to root_url
else
render 'edit'
end
end
它對我來說很好用,你怎么看?
為此,您可以使用 cancan 功能。 它很干凈,並提供了許多功能來添加/修改權限。
看一看
https://github.com/ryanb/cancan
還有一個關於如何使用它的教程
http://railscasts.com/episodes/192-authorization-with-cancan
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.