Ruby on rails Rails3ActiveAdmin。如果未登录,则重定向
我在ActiveAdmin中有一个自定义控制器,允许根据用户角色显示按钮。我在app/admin/invoices.rb文件中执行此操作Ruby on rails Rails3ActiveAdmin。如果未登录,则重定向,ruby-on-rails,ruby,activeadmin,Ruby On Rails,Ruby,Activeadmin,我在ActiveAdmin中有一个自定义控制器,允许根据用户角色显示按钮。我在app/admin/invoices.rb文件中执行此操作 controller do load_and_authorize_resource :except => :index def scoped_collection end_of_association_chain.accessible_by(current_ability) end def action_metho
controller do
load_and_authorize_resource :except => :index
def scoped_collection
end_of_association_chain.accessible_by(current_ability)
end
def action_methods
['index'] + (current_admin_user.role=="administrator" ? ['edit','update','new','create','destroy', 'show'] : ['show'])
end
end
如果用户未登录,我将收到此错误
NoMethodError in Admin::InvoicesController#index
undefined method `role' for nil:NilClass
我如何重定向到登录页面admin\u root\u路径?我也测试过类似的东西
def action_methods
if current_admin_user.nil?
redirect_to admin_root_path
elsif current_admin_user.role == "administrator"
['index', 'edit','update','new','create','destroy', 'show']
elsif current_admin_user.role == "customer"
['index']
else
end
end
我得到了这个错误
AbstractController::ActionNotFound (AbstractController::ActionNotFound):
AdminUser类AdminUser.rb
class AdminUser < ActiveRecord::Base
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me,
:customer_id, :role
validates :customer_id, :presence => true, :if => :is_customer?
belongs_to :customer
after_create { |admin| admin.send_reset_password_instructions }
def password_required?
new_record? ? false : super
end
def is_customer?
self.role == 'customer'
end
before_create :set_new_user_as_customer
def set_new_user_as_customer
self.role = 'customer'
end
end
class Ability
include CanCan::Ability
def initialize(user)
user ||= AdminUser.new
if user.role == "administrator"
can :manage, :all
elsif user.role == "customer"
cannot :create, :all
cannot :update, :all
cannot :destroy, :all
can :read, Shipment, :customer_id => user.customer_id
can :index, Invoice, :customer_id => user.customer_id
else
cannot :manage, :all
end
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
# Override build_footer method in ActiveAdmin::Views::Pages
require 'active_admin_views_pages_base.rb'
rescue_from CanCan::AccessDenied do |exception|
redirect_to admin_custom_dashboards_path, :alert => exception.message
end
def after_sign_in_path_for(resource_or_scope)
admin_custom_dashboards_path
end
def current_ability
@current_ability ||= Ability.new(current_admin_user)
end
end
ActiveAdmin.register Invoice do
menu :if => proc{ can?(:manage, Invoice) }, :priority => 2
controller do
load_and_authorize_resource :except => :index
def scoped_collection
end_of_association_chain.accessible_by(current_ability)
end
def action_methods
['index'] + (current_admin_user.role=="administrator" ? ['edit','update','new','create','destroy', 'show'] : ['show'])
end
end
...
应用程序\u控制器.rb
class AdminUser < ActiveRecord::Base
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me,
:customer_id, :role
validates :customer_id, :presence => true, :if => :is_customer?
belongs_to :customer
after_create { |admin| admin.send_reset_password_instructions }
def password_required?
new_record? ? false : super
end
def is_customer?
self.role == 'customer'
end
before_create :set_new_user_as_customer
def set_new_user_as_customer
self.role = 'customer'
end
end
class Ability
include CanCan::Ability
def initialize(user)
user ||= AdminUser.new
if user.role == "administrator"
can :manage, :all
elsif user.role == "customer"
cannot :create, :all
cannot :update, :all
cannot :destroy, :all
can :read, Shipment, :customer_id => user.customer_id
can :index, Invoice, :customer_id => user.customer_id
else
cannot :manage, :all
end
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
# Override build_footer method in ActiveAdmin::Views::Pages
require 'active_admin_views_pages_base.rb'
rescue_from CanCan::AccessDenied do |exception|
redirect_to admin_custom_dashboards_path, :alert => exception.message
end
def after_sign_in_path_for(resource_or_scope)
admin_custom_dashboards_path
end
def current_ability
@current_ability ||= Ability.new(current_admin_user)
end
end
ActiveAdmin.register Invoice do
menu :if => proc{ can?(:manage, Invoice) }, :priority => 2
controller do
load_and_authorize_resource :except => :index
def scoped_collection
end_of_association_chain.accessible_by(current_ability)
end
def action_methods
['index'] + (current_admin_user.role=="administrator" ? ['edit','update','new','create','destroy', 'show'] : ['show'])
end
end
...
current_admin_user
是ruby类的对象
您可以发布该类(包含role
方法)的内容吗
我认为,这个对象没有正确初始化
您在if-else检查时出错。请再次仔细检查。您似乎需要通过设置当前管理员用户变量使当前用户对象对视图可用。当前
当前管理员用户
为零,因此显然未定义它。请在会话控件中尝试以下操作r、 如果你有
def get_user
current_admin_user = session[:current_user]
end
action\u方法的预期结果是一个操作名称数组,因此,当您尝试从该方法返回重定向时,您应该预期会出现异常。您应该确保您有一个登录用户使用before筛选器(例如before\u筛选器:authenticate\u user!
)
我要检查的另一件事(因为我通常没有使用ActiveAdmin或抽象控制器)是确保您的抽象控制器(controller do…end
)有权访问设计控制器方法-否则,加载和授权资源,验证用户!
等将失败。我在问题中添加了应用程序\u controller.rb文件。不应该先查看应用程序\u controller.rb,然后再查看invoices.rb吗?如果是这样,那么在应用程序\u controller.rb中即使用户为nil@current_ability | |=ability.new(current_admin_user),也会创建该用户
那么我是如何得到NilClass错误的?未定义的nil:NilClass的方法角色
嘿,我已经发布了包含角色方法的类的内容,它是AdminUser类。请有人帮助。你能发布Admin::InvoicesController
的内容吗?修改后的答案:发布了活动的管理员发票文件,我没有一个传统的发票控制器,因为我让active admin管理它。你能将代码发布到定义了当前\u admin\u用户
的地方吗?我想你有一个会话帮助器或执行此操作的东西。AbstractController::ActionNotFound
错误也可能与Desive连接(谷歌搜索显示Desive有很多错误)。ActiveAdmin正在用Desive和CanCan处理它。我没有会话控制器