2017-10-04 25 views
0

我这样做是在routes.rbRails:before_action:authenticate_admin!不工作

devise_for :admins, :skip => [:registrations], controllers: {sessions: 'admins/sessions'} 
devise_scope :admin do 
    get "admins/home"=> "admins/sessions#home", :as => "admin_home" 
end 

但是,当我把before_action :authenticate_admin!的会话控制器,并打开admins/home登录屏幕不显示,没有任何错误弹出都不是。我无需登录即可完全访问该页面!

class Admins::SessionsController < Devise::SessionsController 
    before_action :configure_sign_in_params, only: [:create] 
    before_action :authenticate_admin! 

    def home 
    end 

    def new 
    super 
    end 


    def create 
    super 
    end 


    def destroy 
    super 
    end 

    protected 

    def configure_sign_in_params 
    devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute]) 
    end 
end 

任何想法我可能做错了什么?

+0

你能在管理员粘贴代码/会话控制器 – krishnar

+0

感谢@krishnar的回复,我上传了控制器。 – Theopap

回答

1

您定义的路线在 action.So改变家庭行动

class Admins::SessionsController < Devise::SessionsController 
    before_action :configure_sign_in_params, only: [:create] 

    def home 
    self.resource = warden.authenticate!(auth_options) 
    set_flash_message!(:notice, :signed_in) 
    sign_in(resource) 
    respond_with resource, location: after_sign_in_path_for(resource) 
    end 

    def new 
    super 
    end 


    # override method here 
    def create 
    end 


    def destroy 
    super 
    end 

    protected 

    def configure_sign_in_params 
    devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute]) 
    end 
end 

注意创建会话,然后把这个应用控制器

before_action :authenticate_admin! 
+0

我仍然可以完全访问该页面而无需登录!当我去'管理员/主页'登录屏幕不会出现,也不会出现错误!另外,我从'sessions_controller.rb'中删除'before_action:authenticate_admin!' – Theopap

+0

@Theopap before_action:authenticate_admin!你应该把它放在应用程序控制器中 – krishnar

+0

我把'application_controller.rb'移到了'application_controller.rb'中,但没有任何改变......我使用'rubyMine',所以这是它只在'RubyMine'里面给出的错误:'期望的方法名从当前或父类到'ActionController :: Base',但找到':authenticate_admin!'。或散列预期' – Theopap