设计authenticate_user

Dmy*_*sin 6 ruby-on-rails devise

帮助我解决这个问题:

我有2个模型(管理员和用户) - >用devise创建,我有post_controller:

问题出现了:

如果我有一个型号(user.rb) - >在我的控制器中我把它:

before_filter :authenticate_user!, :except => [:show, :index]  
Run Code Online (Sandbox Code Playgroud)

但我有2个模型,我希望用户可以访问帖子控制器的"显示"和"索引"操作,管理员可以访问所有操作.

我做了类似的事情:

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

          else 
            authenticate_user!
          end   
        end
Run Code Online (Sandbox Code Playgroud)

但我想改变我的字符串:

authenticate_user!

这样的事情:

:authenticate_user!, :except => [:show, :index]
但除了指的是before_filter之外

我怎么能这样做(没有'cancan'宝石)

ron*_*chn 15

尝试使用两个前过滤器 - 一个用于管理员操作,另一个用于管理员或用户操作.

# 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
Run Code Online (Sandbox Code Playgroud)