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:
Here are my routes:
devise_for :users, controllers: {
sessions: 'users/sessions',
confirmations: 'users/confirmations',
registrations: 'users/registrations',
passwords: 'users/passwords',
invitations: 'users/invitations'
}, sign_out_via: [:get, :delete]
And /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
If the resource is not persisted, in the spot where it says render :new I've tried the following:
redirect_to new_user_registration_path(resource)
- does not persist errors
render :new
- routes to '/users' but does persist errors
respond_with resource
- does same as :new
respond_with resource, location: new_user_registration_path(resource)
- again same as above
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.
Thanks a lot
From reading your question, im assuming you want to go back if registration fails?
you can use:
redirect_to :back
And in your code like this:
else
clean_up_passwords resource
render :new
redirect_to :back
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.