2016-08-22 21 views
0

目前,在我的用戶控制我的編輯動作,像這樣:訪問用戶編輯頁面,當你聯繫

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

這個作品,如果我要訪問我自己的用戶配置文件。但是,如果我想訪問其他人的用戶配置文件,這是行不通的。我應該如何改變這一點?

+0

「如果我想訪問別人的用戶配置文件,這是行不通的。」但它確實如此。 '/ users/1/edit' =編輯用戶1.你想要的是鎖定編輯,以便非管理員*不能*編輯。 – coreyward

+0

哇,我不知道我爲什麼這麼想。 – nachime

回答

1

添加一個動作check_right_user之前,它檢查當前用戶是否嘗試訪問他自己的配置文件。

before_action :check_admin, only: [:edit, :update, :destroy] 


def check_admin 
    unless current_user.admin? 
    redirect_to root_path, alert: "You're not authorized" 
    end 
end 

我假設你有你的user模型在application_controllerusers_controlleradmin場限定的current_user方法。

+0

我的不好。問題是我希望管理員能夠訪問其他用戶的編輯操作。 – nachime

+0

@nachime更新。這是你想要的嗎? –