Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:before\u操作:验证\u管理员!不';行不通_Ruby On Rails_Ruby_Ruby On Rails 4_Devise - Fatal编程技术网

Ruby on rails Rails:before\u操作:验证\u管理员!不';行不通

Ruby on rails Rails:before\u操作:验证\u管理员!不';行不通,ruby-on-rails,ruby,ruby-on-rails-4,devise,Ruby On Rails,Ruby,Ruby On Rails 4,Devise,我在routes.rb中执行此操作: devise_for :admins, :skip => [:registrations], controllers: {sessions: 'admins/sessions'} devise_scope :admin do get "admins/home"=> "admins/sessions#home", :as => "admin_home" end 但是当我把放在\u操作之前:authenticate\u admin并打开管

我在
routes.rb中执行此操作:

devise_for :admins, :skip => [:registrations], controllers: {sessions: 'admins/sessions'}
devise_scope :admin do
  get "admins/home"=> "admins/sessions#home", :as => "admin_home"
end
但是当我把
放在\u操作之前:authenticate\u admin并打开
管理员/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
class Admins::SessionController

你知道我可能做错了什么吗?

你定义的在home action中创建会话的路线。因此更改home action

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

你能在admins/sessions controller中粘贴代码谢谢回复@krishnar,我上传了控制器。我仍然可以完全访问该页面而不必登录!!当我进入
admins/home
时,登录屏幕不会出现,也不会弹出错误!此外,我还删除了操作之前的
:authenticate\u admin来自
会话\u controller.rb
@Theopap\u操作之前:验证\u admin!你应该把它放在应用程序控制器里我把它移到了
应用程序\u controller.rb
但是没有任何变化。。。我使用的是'rubyMine',所以这是它只在
rubyMine
中给我的错误:
从当前或父类到'ActionController::Base'的预期方法名,但找到了:'authenticate\u admin!'。或者hash expected
我只需将代码放入
def home
而不是
def create
方法中,然后在执行以下操作之前删除
:authenticate\u admin来自
应用程序\u控制器
!谢谢你的帮助!!!既然你找到了90%的解决方案,你可以继续回答这个问题,我会接受的。@Theopap好的,现在我看到你已经改变了会话路线。这是一个“打到家”的动作。很抱歉
before_action :authenticate_admin!