我希望让 Active Admin 为网站做后端。不过,我真的不想为 Users 和 AdminUsers 使用单独的模型。
如果用户模型中有 is_admin 标志,我能否只让管理员用户登录到 Active Admin?如果用户不是管理员,他们应该只能登录到网站前面的简单控制面板。
最佳答案
您可以按照您的建议将 is_admin
标志添加到您的 User
模型。然后,您可以使用 is_admin?
条件围绕您的每个管理选项,并在您的 Controller 中使用一些 before_filter :admin_required
。
application_controller.rb:
# if user is not admin redirect to main page
def admin_required
current_user.is_admin? || redirect_to("/")
end
any_controller.rb:
# Everybody can access show and index action, all others require admin flag set
before_filter :admin_required, :except => [:show, :index]
any_view/show.html.erb
<% if current_user.is_admin? %>
Hi Admin!
Some cool admin stuff
<% else %>
Hi User!
<% end %>
Stuff for everybody
关于ruby-on-rails - Active Admin - 用户和管理员的相同模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8268730/