0
votes

I am trying to create a simple website with a landing page and a user registration form on the landing page itself.

I am having trouble figuring out how to change the devise registration controller to redirect back to the landing page (root path "/") when there is a form error during the registration (such as a missing field).

Right now, if there is a successful registration, the form works properly. However, if there is an error in the registration, it redirects to /users/sign_up view which only contains the registration form. I need it to redirect/render the landing page in the event that there is an error during the registration.

I have generated a custom registration controller, however right now it only calls super.

# POST /resource
def create
  super
end

I have also looked at after_sign_up_path_for, however that does not work since that path is only called upon a successful registration.

I suspect that the solution might be a simple one given that this sort of design pattern can be quite common, however I cant figure out how to do it.

1

1 Answers

1
votes

You can copy the devise registrations controller from here

You should be able to add something like this:

app/controllers/registrations_controller.rb

   class RegistrationsController < Devise::RegistrationsController
      def new
        super
      end

      def create
        build_resource(sign_up_params)
        if resource.save
          #respond_with resource, location: root_path
          return
        else
          #respond_with resource, location: lending_page_path
          return
        end
        yield resource if block_given?
        if resource.persisted?
          if resource.active_for_authentication?
            set_flash_message! :notice, :signed_up
            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}"
            expire_data_after_sign_in!
            respond_with resource, location: after_inactive_sign_up_path_for(resource)
           end
         else
          clean_up_passwords resource
          set_minimum_password_length
          respond_with resource
         end
      end
  end

Then in your routes:

devise_for :users, :controllers => {:registrations => 'registrations'}