Ruby on rails 设计:如果没有用户,则重定向到注册页面,而不是登录
如果没有用户在场,我希望将请求重定向到注册页面而不是登录页面。因此,我将覆盖Ruby on rails 设计:如果没有用户,则重定向到注册页面,而不是登录,ruby-on-rails,redirect,devise,ruby-on-rails-5,Ruby On Rails,Redirect,Devise,Ruby On Rails 5,如果没有用户在场,我希望将请求重定向到注册页面而不是登录页面。因此,我将覆盖authenticate\u用户应用程序控制器中的方法: class ApplicationController < ActionController::Base protect_from_forgery with: :exception before_action :authenticate_user! protected def authenticate_user! redirect
authenticate\u用户代码>应用程序控制器中的方法:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
protected
def authenticate_user!
redirect_to new_user_registration_path unless User.any?
end
end
工作以及为我,我已经为我和我现有的项目固定
在application\u controller.rb上,为每个请求运行,并在覆盖authenticate\u用户之后运行代码>和回调后在\u操作之前:验证\u用户代码>整个控制器调用此方法,也可从会话控制器
和注册控制器
调用此方法,如果未登录
,如果您在单独的控制器上使用它
,那么一切都很好,就像您在主控制器.rb上使用它一样,然后在请求主控制器.rb
而没有登录
时,他将参考登录
页面
经过多次搜索,我已经修复了如下所示
在应用程序.rb上
我已阻止,我的意思是在筛选之前跳过,设计控制器,用于登录
,注销
和注册
,如下所示
config.to_prepare do
Devise::SessionsController.skip_before_action :authenticate_user!
Devise::RegistrationsController.skip_before_action :authenticate_user!
end
以及应用程序\u controller.rb
before_action :authenticate_user!
protected
def authenticate_user!
redirect_to new_user_registration_path, notice: 'Your custom message here' unless user_signed_in?
end
config.to_prepare do
Devise::SessionsController.skip_before_action :authenticate_user
Devise::RegistrationsController.skip_before_action :authenticate_user
end
before_action :authenticate_user
protected
def authenticate_user
redirect_to new_user_registration_path, notice: 'Your custom message here' unless user_signed_in?
end
就这样
注意
在此之后,一切正常,但设计
默认值编辑
和更新
无效
为此,您必须应用解决方案2
解决方案2
设计
默认值编辑
和更新
如果删除此,将起作用代码>来自验证用户
如下面的应用程序.rb
before_action :authenticate_user!
protected
def authenticate_user!
redirect_to new_user_registration_path, notice: 'Your custom message here' unless user_signed_in?
end
config.to_prepare do
Devise::SessionsController.skip_before_action :authenticate_user
Devise::RegistrationsController.skip_before_action :authenticate_user
end
before_action :authenticate_user
protected
def authenticate_user
redirect_to new_user_registration_path, notice: 'Your custom message here' unless user_signed_in?
end
以及应用程序\u controller.rb
before_action :authenticate_user!
protected
def authenticate_user!
redirect_to new_user_registration_path, notice: 'Your custom message here' unless user_signed_in?
end
config.to_prepare do
Devise::SessionsController.skip_before_action :authenticate_user
Devise::RegistrationsController.skip_before_action :authenticate_user
end
before_action :authenticate_user
protected
def authenticate_user
redirect_to new_user_registration_path, notice: 'Your custom message here' unless user_signed_in?
end
如果应用此选项,请确保在编辑application.rb
文件后重新启动服务器
希望它有帮助designe不建议覆盖authenticate\u用户
和authenticate\u用户代码>方法
相反,请使用route方法定义自定义故障应用程序,该方法返回表示要重定向到的命名路由的符号:
# app/lib/my_failure_app.rb
class MyFailureApp < Devise::FailureApp
def route(scope)
:new_user_registration_url
end
end
如果出现未初始化的常量CustomFailure错误,并且已将CustomFailure类放在/lib目录下,请确保在application.rb文件中自动加载lib文件,如下所示
config.autoload_paths << Rails.root.join('lib')
config.autoload\u路径是否存在User
记录?你重定向到哪里?重定向发生在注册页面上,但无限。在你的问题中包含这些信息是值得的,这有点重要。你能显示“rails路由”的结果吗?