Dof*_*ofs 7 ruby-on-rails devise
我想要在我的Rails 4网站上的所有页面上登录.在我添加的ApplicationController中before_action :authenticate_user!,但它根本不做任何事情.我试图将其添加before_action :authenticate_user!到另一个控制器,它工作正常.
我是否需要对ApplicationController执行其他操作,以使所有操作都需要登录(除了注册/登录)?
Ric*_*eck 17
这是我们使用的实际代码:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
#Actions
before_action :authenticate_user! #-> routes to the login / signup if not authenticated
end
Run Code Online (Sandbox Code Playgroud)
你可能遇到的问题有两个:
-
确保您的其他控制器继承自application_controller:
#app/controllers/other_controller.rb
Class OtherController < ApplicationController
...
end
Run Code Online (Sandbox Code Playgroud)
-
你以某种方式"跳过" before_action回调
如果您在skip_before_action任何地方使用,则需要将其删除.它可能会导致您的authenticate_user!方法出现问题.要解决这个问题,我首先测试没有任何skip_before_action回调,然后看看是什么让它正常工作
进一步说明 - signin / signup无所谓.它们都继承自Devise控制器; 他们只是按要求运行.