在失败的注册时,Rails设计重定向到’/ users’而不是’/ users / sign_up’

我一直在寻找解决这个问题 – 当用户使用Devise创建注册失败时,无论出于何种原因,他们都会被重定向到’/ users’而不是留在’/ users / sign_up’并持续存在错误:

这是我的路线:

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 

如果资源没有持久化,则在它所说的渲染点:new我尝试了以下内容:

redirect_to new_user_registration_path(resource) – 不会保留错误

render :new – 到’/ users’的路由但是仍然存在错误

respond_with resource – 与:new相同

respond_with resource, location: new_user_registration_path(resource) – 再次与上面相同

因此,如果资源不存在,我怎样才能让它回到同一页面 – ‘/ users / sign_up’并坚持错误? 我已经看到了一些非常hacky的方式,但是Devise没有办法为此提供解决方案,而不是强迫它路由到’/ users’。 我也看到了包含重定向和设置闪存的解决方案 – 这不是我想要做的事情,我宁愿错误仍然存​​在于表单中。

非常感谢

从阅读你的问题,我假设你想要回去,如果注册失败?

您可以使用:

 redirect_to :back 

在你的代码中这样:

 else clean_up_passwords resource render :new redirect_to :back end