帮助我解决这个问题:
我有 2 个模型(管理员和用户)-> 用设计创建,
我有 post_controller:
问题出现了:
如果我有一个模型( user.rb )-> 在我的 Controller 中,我把它:
before_filter :authenticate_user!, :except => [:show, :index]
但我有 2 个模型,我希望用户可以访问 post Controller 的“显示”和“索引”操作,而管理员可以访问所有操作。
我做这样的事情:
before_filter :logged_in
.
.
.
private
def logged_in
if admin_signed_in?
else
authenticate_user!
end
end
但我想改变我的字符串:
authenticate_user!
到这样的事情:
:authenticate_user!, :except => [:show, :index]
但除了指的是 before_filter我该怎么做(没有'cancan' gem)
最佳答案
尝试使用两个前置过滤器 - 一个仅用于管理员操作,另一个用于管理员或用户操作。
# 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
关于ruby-on-rails - 设计 authentication_user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12278534/