Rails无法成功使用before_filter进行管理

时间:2014-03-28 20:18:13

标签: ruby-on-rails activeadmin before-filter

我尝试使用before_filter作为管理页面,我有登录页面和仪表板等。

所以这是我的admin_user.rb

 ActiveAdmin.register AdminUser do     
index do                            
column :email                     
column :current_sign_in_at        
column :last_sign_in_at           
column :sign_in_count             
default_actions                   
end                                 

 filter :email                       

form do |f|                         
  f.inputs "Admin Details" do       
  f.input :email                  
  f.input :password               
  f.input :password_confirmation  
  end                               
f.actions                         
end                                 

并且,在我想要保护的页面的控制器中,我已经把它放在首位:

before_filter check_if_admin


 def check_if_admin
    if signed_in?
  #    raise 'Only admins allowed!' unless current_user.admin?
    raise 'Only admins allowed!'
 else
raise 'Please sign in!'
 end
end

所以我收到了一个错误:

undefined method `model_name' for NilClass:Class

来自我的控制器的de _form.html.erb ...

我该怎么做?我在管理员处访问某个页面。

谢谢!

注意:我是网络开发的初学者,对不起如果这对您来说很容易:)

编辑

模型admin_user.rb

class AdminUser < ActiveRecord::Base
  # Include default devise modules. Others available are:
   # :confirmable, :lockable, :timeoutable and :omniauthable
   devise :database_authenticatable, 
     :recoverable, :rememberable, :trackable, :validatable

   # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
    # attr_accessible :title, :body
   end

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

试试这个方法

def check_if_admin
    if current_admin_user_signed_in?
     raise 'Welcome Admin'
    else
    raise 'Only admins allowed!'
 end
end