如何在注册失败时将用户重定向到特定页面?
How can I redirect a user to a specific page when registration fails?
我有一个 Rails 应用程序,它使用 Devise 进行用户注册/身份验证。注册表单和登录表单都在我的域的根目录中。
当用户注册失败时(例如因为他们输入了一个已经被占用的电子邮件地址),默认情况下 Devise 重定向到 /users
.
我该如何改变它?我希望用户被定向到 /
我已经成功地实现了登录尝试失败,代码如下:
class CustomFailure < Devise::FailureApp
def redirect_url
"/"
end
def respond
if http_auth?
http_auth
else
redirect
end
end
end
和:
config.warden do |manager|
manager.failure_app = CustomFailure
end
有什么方法可以扩展/改变这一点,以便失败的注册也重定向到我的域的根目录?
我正在使用 Ruby 2.2.0、Rails 4.2.0 和 Devise 3.4.1。
您可能需要继承 Devise::RegistrationsController
并覆盖创建操作。只需从 here 复制创建方法并修改保存失败时的重定向。
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
build_resource
if resource.save
set_flash_message :notice, :inactive_signed_up, :reason => inactive_reason(resource) if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
#end
else
clean_up_passwords(resource)
respond_with_navigational(resource) { render_with_scope :new }
end
end
end
# The path used after sign up for inactive accounts. You need to overwrite
# this method in your own RegistrationsController.
def after_inactive_sign_up_path_for(resource)
new_user_session_path
end
更改您的路线以告知 Devise 使用您的控制器:
# config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
相信你可以看看this问题。您可以重写 Devise RegistrationsController 并将您的 redirect_to
方法添加到未保存用户时的 else
。
例如:
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
if @user.save?
#something
else
redirect_to your_path, error: 'Registration failed'
end
我有一个 Rails 应用程序,它使用 Devise 进行用户注册/身份验证。注册表单和登录表单都在我的域的根目录中。
当用户注册失败时(例如因为他们输入了一个已经被占用的电子邮件地址),默认情况下 Devise 重定向到 /users
.
我该如何改变它?我希望用户被定向到 /
我已经成功地实现了登录尝试失败,代码如下:
class CustomFailure < Devise::FailureApp
def redirect_url
"/"
end
def respond
if http_auth?
http_auth
else
redirect
end
end
end
和:
config.warden do |manager|
manager.failure_app = CustomFailure
end
有什么方法可以扩展/改变这一点,以便失败的注册也重定向到我的域的根目录?
我正在使用 Ruby 2.2.0、Rails 4.2.0 和 Devise 3.4.1。
您可能需要继承 Devise::RegistrationsController
并覆盖创建操作。只需从 here 复制创建方法并修改保存失败时的重定向。
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
build_resource
if resource.save
set_flash_message :notice, :inactive_signed_up, :reason => inactive_reason(resource) if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
#end
else
clean_up_passwords(resource)
respond_with_navigational(resource) { render_with_scope :new }
end
end
end
# The path used after sign up for inactive accounts. You need to overwrite
# this method in your own RegistrationsController.
def after_inactive_sign_up_path_for(resource)
new_user_session_path
end
更改您的路线以告知 Devise 使用您的控制器:
# config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
相信你可以看看this问题。您可以重写 Devise RegistrationsController 并将您的 redirect_to
方法添加到未保存用户时的 else
。
例如:
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
if @user.save?
#something
else
redirect_to your_path, error: 'Registration failed'
end