设计:注册失败时重定向? [英] Devise: redirect on sign up failure?
问题描述
我正在尝试重定向未通过注册表单的用户(例如,他们输入的用户名已被占用,他们将字段留空等...)
I'm trying to redirect users that have failed the sign up form (e.g. they entered a username that is already taken, they left a field blank, etc...)
我为登录表单失败的用户设置了自定义失败,代码如下:
I have custom failure set up for users that fail the sign in form, code below:
class CustomFailure < Devise::FailureApp
def redirect_url
root_path
end
def respond
if http_auth?
http_auth
else
redirect
end
end
但是,我一直不知道如何设置它以防止注册失败.理想情况下,我只想将它们重定向回/到 root_path,有什么想法吗?谢谢!
However, I've been stuck on how to set this up for sign up failure. Ideally I would just like to redirect them back/to root_path, any ideas? Thank you!
推荐答案
您可能需要继承 Devise::RegistrationsController
并覆盖 create 操作.只需从 here 复制 create 方法并修改保存失败时的重定向.
You will probably need to subclass Devise::RegistrationsController
and override the create action. Just copy over the create method from here and modify the redirect on failure to save.
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
# modify logic to redirect to root url
end
end
更改您的路线以告诉 Devise 使用您的控制器:
Change your routes to tell Devise to use your controller:
# config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
这篇关于设计:注册失败时重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!