2012-01-31 57 views
1

我在ActiveAdmin中有这个自定义控制器,允许根据用户角色显示按钮。我这样做是在应用程序/管理/ invoices.rb文件Rails 3 ActiveAdmin。如果没有登录,重定向

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_root_path代替?我还测试了这样的事情...

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.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 

的能力类ability.rb

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 

application_controller.rb

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 

/app/admin/invoices.rb

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 
    ... 
+0

你可以发布'current_admin_user'定义的代码吗?我想你有一个session_helper或执行此操作的东西。 'AbstractController :: ActionNotFound'错误也可能连接到设计(使用Google搜索它显示负载此错误)。 – HectorMalot 2012-02-08 13:10:14

回答

0

current_admin_user是红宝石类的对象。

你能张贴类的内容(包含role方法?

我认为,这个对象是不正确初始化。

你正在做一些错用的if-else检查。检查此

+0

谢谢!添加更多详细信息 – leonel 2012-01-31 00:36:11

+0

我在我的问题中添加了application_controller.rb文件。不应该先查看application_controller.rb然后invoices.rb?如果是这种情况,那么在application_controller.rb中创建一个用户,即使它是nil @current_ability || = Ability.new(current_admin_user)'那么它是如何得到一个NilClass错误? 'nil的未定义方法角色:NilClass' – leonel 2012-01-31 15:30:32

+0

嘿,我已经发布了包含角色方法的类的内容,它是AdminUser类。请有人帮忙。 – leonel 2012-02-08 05:41:50

0

看起来你需要通过设置你当前的管理用户变量来使当前用户对象可用于视图当前current_admin_user是零,所以它显然没有被定义在你的会话中尝试类似以下内容控制器,如果你有。

def get_user 
    current_admin_user = session[:current_user] 
end 
+0

ActiveAdmin正在使用Devise和CanCan处理它。我没有会议控制器 – leonel 2012-02-09 20:16:52

0

action_methods预期的结果是动作名称的数组,所以当你尝试从该方法返回一个重定向,你应该期待一个例外。您应该确保您拥有使用过滤器前登录的用户(例如before_filter :authenticate_user!)。

另一件事我会检查(因为我没有使用ActiveAdmin或一般的抽象控制器)为确保您的一个AbstractController(controller do ... end)先后获得了设计控制器方法 - 否则load_and_authorize_resourceauthenticate_user!等将失败。