2012-09-05 18 views
6

助け、私にその質問に: -工夫authenticate_user

iが2機種(管理者とユーザーを)持っている私はpost_controller持っている>工夫、 で作成し、:

をし、疑問が生じる:

場合私は1つのモデル(user.rb)持っている - >私のコントローラで私は置く:私は2つのモデルを持っている

before_filter :authenticate_user!, :except => [:show, :index] 

けどをして、私は「ショー」と「インデックス」アクション0へのアクセス権を持っているユーザーにしたいですfポストコントローラと管理者はすべての操作にアクセスできます。

と私はそのような何か:そのような何かに

authenticate_user!

before_filter :logged_in 
. 
. 
. 
    private 
     def logged_in 
      if admin_signed_in? 

      else 
      authenticate_user! 
      end 
     end 

しかし、私は私の文字列を変更したい

:authenticate_user!, :except => [:show, :index]
が、以外が参照をbefore_filter

どうすればいいですか(cancan宝石なし)

答えて

13

管理者用のアクションと管理者アクションまたはユーザーアクションの2つのフィルタを使用してみてください。

# ensure admin for other actions 
before_filter :check_admin_logged_in!, :except => [:show, :index] 

# ensure user or admin logged in for these actions (:only option is optional) 
before_filter :check_user_logged_in!, :only => [:show, :index] 

private 
    def check_admin_logged_in! # admin must be logged in 
     authenticate_admin! 
    end 
    def check_user_logged_in! # if admin is not logged in, user must be logged in 
     if !admin_signed_in? 
     authenticate_user! 
     end 
    end