2016-05-18 4 views
-1

私に親切に助言を与えることができました - 非常に感謝します。私のusers_controller.rbファイルには、edit actionの中に条件文があります。私は、コードcurrent_user.company == @user.company && current_user.firstname == @user.firstnameのこのラインを書くためにしたいと思い、私モデルのcurrent_userとuser_idを比較する方法 - Rails4

users_controller.rbファイルにuser.rbモデルファイルからメソッド profile_editing_rights呼び出すしたいと思いますusers_controller.rb

def edit 
    if current_user.company == @user.company && current_user.firstname == @user.firstname 
    # can edit only their profile 
    else 
    redirect_to access_restricted_path 
    end 
end 

user.rbモデルファイルしかし、私は以下試みたが、動作しませんどのように

わからない午前:

def profile_editing_rights 
self.company == user_id.company && self.firstname == user_id.firstname 
end 

1は親切に私は以下のようにusers_controller.rbファイル でそれを呼び出すことができるように正しく(user.rbファイル内)profile_editing_rights メソッドを作成する方法を私に助言することができます:

def edit 
    if current_user.profile_editing_rights 
    # can edit only their profile 
    else 
    redirect_to access_restricted_path 
    end 
end 

はるかにUSER_IDが整数で、オブジェクトではないようだ

+0

ヤホールが示唆しているように、それを前のフィルターに入れてください。フィルタの内部でリダイレクトを処理することもできます。 – venkatKA

答えて

1
def profile_editing_rights 
self.company == user_id.company && self.firstname == user_id.firstname 
end 

を高く評価しました。 モデルではcurrent_userは使用できません。

あなたのメソッドのパラメータとしてCURRENT_USER値を置くことができますが、それはコントローラに権利をチェックすると良いでしょう(多分before_filter(アクション)を書き込む)

また、あなたは、いくつかの認証チェッカー宝石を使用することができます。例えば「評論家'

関連する問題