如果注册失败,Rails会重定向到"/users"而不是"/users/sign_up" [英] Rails devise redirecting to '/users' instead of '/users/sign_up' on failed registration
问题描述
我一直在搜索以解决此问题-当用户使用Devise创建注册失败时,无论出于何种原因,他们都被重定向到'/users'而不是停留在'/users/sign_up'上并保留错误:
I've been searching all over to fix this - when a user fails a registration create using Devise, for whatever reason they get redirected to '/users' instead of staying on '/users/sign_up' and persisting the errors:
这是我的路线:
devise_for :users, controllers: {
sessions: 'users/sessions',
confirmations: 'users/confirmations',
registrations: 'users/registrations',
passwords: 'users/passwords',
invitations: 'users/invitations'
}, sign_out_via: [:get, :delete]
和/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
prepend_before_filter :require_no_authentication, only: [:new, :create]
def new
@body_class = "hold-transition register-page"
build_resource({})
yield resource if block_given?
respond_with resource
end
def create
build_resource(sign_up_params)
resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_in(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
render :new
end
end
end
如果资源没有持久保存,则在该资源显示render:new的地方尝试了以下操作:
If the resource is not persisted, in the spot where it says render :new I've tried the following:
重定向到new_user_registration_path(资源)
-不存在错误
render:new
-路由到'/users'但确实存在错误
render :new
- routes to '/users' but does persist errors
有资源的响应
-与:new
带有资源的响应,位置:new_user_registration_path(资源)
-与上面相同
因此,如果资源没有持久保存,我如何才能回到原来的页面-'/users/sign_up'并持久保存错误?我已经看到了一些非常骇人听闻的方法,但是Devise没有办法为此提供解决方案,而没有强迫它路由到"/用户".我还看到了包括重定向和设置Flash的解决方案-这不是我想要做的,我宁愿错误仍然存在于表单中.
So if the resource doesn't persist how can I get it to just go back to the same page - '/users/sign_up' and persist the errors? I've seen some very hacky ways but there's no way Devise doesn't offer a solution for this instead of forcing it to route to '/users'. I've also seen solutions that include a redirect and setting a flash - this is not something I want to do, I'd rather the errors persist in the form.
非常感谢
推荐答案
通过阅读您的问题,我是否假设您想在注册失败后返回?
From reading your question, im assuming you want to go back if registration fails?
您可以使用:
redirect_to :back
在这样的代码中:
else
clean_up_passwords resource
render :new
redirect_to :back
end
这篇关于如果注册失败,Rails会重定向到"/users"而不是"/users/sign_up"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!