UserSessionモデルを通じてUser.role_idを引くことができますだから、私はUserモデル、Role Model、User Sessionsモデルを持っています。ユーザーはacts_as_authenticatedです。私application_controllerでどのように私は私のアプリでうまくauthlogic実行しているが、私は私自身の役割(私はレールに新しいですし、学びたいと思っ...)</p> <p>を転がしています
protect_from_forgery
helper_method :current_user, :is_admin, :is_group_coach, :is_group_leader
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
def is_admin
current_user.role_id == 3
end
def is_group_coach
current_user.role_id == 2
end
def is_group_leader
current_user.role_id == 1
end
それから私は、ビュー内であれば、単純なis_adminをしています...
が、nilのためにその返す未定義のメソッド `ROLE_ID」:NilClass
私は思いますcurrent_userが実際にはUserSessionではなくUserSessionモデルで実行されているため、これを実行しています...これを変更して、期待どおりに動作させるにはどうすればよいですか?パラメータなしを見つけるあなたはを呼び出すことはできませんよう
あなたのユーザーはログインしていません。ログインしたユーザーを必要とするアクションを保護するために 'before-filter'を追加してください。また、あなたのコードのどこでも役割をチェックするのではなく、 'CanCan' gemを使うことを検討してください。 – RocketR