Richlewis
Richlewis

Reputation: 15374

Devise Error redirects to incorrect form

I have just noticed a problem with my devise sign up forms when a validation error is thrown

I have two forms, one for an individual and one for a company, to differentiate the two

<p><%= link_to 'Sign Up as Individual', new_user_registration_path %></p>
<p><%= link_to 'Sign Up as Vets/Rescue Centre', new_user_registration_path(organization: true) %></p>

View

<% if params[:organization] %>
  <%= render 'shared/company_signup_form' %>
<% else %>
  <%= render 'shared/individual_signup_form' %>
<% end %>

When validation fails on the company_signup_form it redirects me to the individal_signup_form (which is the default url for a new user registration)

Is there a way to tell the form to redirect to the form that is being used?

EDIT

I have overridden one method in the registrations controller

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
   administration_index_path
  end
end

Would this cause an issue here ?

Thanks

Upvotes: 0

Views: 353

Answers (1)

knotito
knotito

Reputation: 1392

for custom redirections you have to override create action on registrations_controller

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    build_resource(sign_up_params)
    resource_saved = resource.save
    yield resource if block_given?
    if resource_saved
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_flashing_format?
        sign_up(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
      @validatable = devise_mapping.validatable?
      if @validatable
        @minimum_password_length = resource_class.password_length.min
      end
      if resource.is_a? Organization
        redirect_to new_user_registration_path(organization: true)
      else
        redirect_to new_user_registration_path
      end
    end
  end
end

EDIT

you need to configure devise routes :

devise_for :users, controllers: { registrations: 'users/registrations' }

Upvotes: 1

Related Questions