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在sessions controller中选择code>并打开管理员/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!