Naresh Kumar P
Naresh Kumar P

Reputation: 4210

If sign up error occurs in Devise Sign up form redirect to same route rails 4

I have customized the devise sign up form to show up in my custom page . I am showing up my registration form in the home page. http://awesomescreenshot.com/0815w1h9b0.

I need to show up the Error at the Top of the Form itself. If i click on Submit button without filling up the fields it is redirecting to the '/users' page with errors. I need to show it up the errors in the same page where i have the form.

application_helper.rb

module ApplicationHelper
  def resource_name
    :user
  end

  def resource_class 
     User 
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

application_controller.rb

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception  
before_action :authenticate_user!
before_filter :configure_devise_params, if: :devise_controller?


        def configure_devise_params
         devise_parameter_sanitizer.for(:sign_up) do |u|
          u.permit(:first_name, :last_name, :gender, :dob, :token, :contact_number, :profile_by, :email, :password, :password_confirmation ,:religion_id ,:caste_id ,:sub_caste_id)
            end
         devise_parameter_sanitizer.for(:account_update) do |u|
          u.permit(:first_name, :last_name, :dob, :contact_number, :profile_by, :email, :current_password)
          end   
        end
     # REDIRECT USER TO OUR CUSTOM PATH AFTER LOGIN
        def after_sign_in_path_for(resource)
            root_path
          end
        def after_update_path_for(resource)
          show_profile_path
        end
        # REDIRECT USER TO OUR CUSTOM PATH AFTER LOGOUT
        def after_sign_out_path_for(resource)
            root_path
        end
    end

routes.rb

Rails.application.routes.draw do 
  devise_for :users
  get '/home'=>'home#index', as: 'home_path'
end

new.html.erb under Devise Views

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>

                <div class="field">
                    <%= f.hidden_field :token, value: rand(13 ** 13), autofocus: true %>
                </div>

                <div class="field">
                    <%= f.label :First_Name %>
                    <%= f.text_field :first_name, autofocus: true, placeholder: 'Enter First Name' %>
                </div>

                <div class="field">
                    <%= f.label :Last_Name %>
                    <%= f.text_field :last_name, autofocus: true, placeholder: 'Enter Last Name' %>
                </div>

                <div class="field">
                    <%= f.label :date_of_birth %>
                    <%= f.text_field :dob, class: 'datepicker', placeholder: 'Enter Date of Birth' %>
                        <script type="text/javascript">
                        $('.datepicker').datepicker()
                        </script>
                </div>

                <div class="field">
                    <%= f.label :gender %>
                    <%= f.radio_button :gender, '1', :checked => true %> <span class="radio-names">Male</span>
                    <%= f.radio_button :gender, '2' %><span class="radio-names">Female</span>
                </div>

                <div class="field">
                    <%= f.label :email %>
                    <%= f.email_field :email, autofocus: true, placeholder: 'Enter Email Address' %>
                </div>

                <div class="field">
                    <%= f.label :profile_password %>
                    <% if @minimum_password_length %>
                    <em>(<%= @minimum_password_length %> characters minimum)</em>
                    <% end %>
                    <%= f.password_field :password, autocomplete: "off", placeholder: 'Enter Password' %>
                </div>

                <div class="field">
                    <%= f.label :confirm_password %>
                    <%= f.password_field :password_confirmation, autocomplete: "off", placeholder: 'Enter Confirm Password' %>
                </div>

                <div class="field">
                    <%= f.label :register_for %>
                    <%= f.select :profile_by, [['Myself', 1], ['Dad', 2], ['Mom', 3], ['Brother', 4], ['Sister', 5], ['Relative', 6], ['Son', 7], ['Daughter', 8]],{},{class: 'select_tag'} %>
                </div>

                <div class="field">
                    <%= f.label :contact_number %>
                    <%= f.text_field :contact_number, autofocus: true, placeholder: 'Enter Contact Number +91' %>
                </div>


                <div class="field">
                    <%= f.label :Religion %>
                    <%= f.select :religion_id, options_from_collection_for_select(Religion.all, :id, :religion),{},{class: 'select_tag'} %>
                </div>

                <div class="field">
                    <%= f.label :caste %>
                    <%= f.select :caste_id, options_from_collection_for_select(Caste.all, :id, :caste_name),{},{class: 'select_tag'} %>
                </div>

                <div class="field">
                    <%= f.label :subcaste %>
                    <%= f.select :sub_caste_id, options_from_collection_for_select(Subcaste.all, :id, :sub_caste_name),{},{class: 'select_tag'} %>
                </div>

                <br>
                <br>

                <div class="actions">
                    <%= f.submit "Register", class: 'register_button' %>
                </div>
<% end %>

Help me how to display up the errors at the same page if some error occurs at sign up process.

Upvotes: 3

Views: 1472

Answers (2)

Thorin
Thorin

Reputation: 2034

you do not need to overwrite devise controller Try this:

create class in lib folder and overwrite devise failure method there like this:

class CustomFailure < Devise::FailureApp
  def redirect_url
     #new_user_registration_path or your_path
  end

  def respond
    if http_auth?
     http_auth
   else
     redirect
   end
 end
end

And put this config/initializers/devise.rb

 config.warden do |manager|
    manager.failure_app = CustomFailure
  end

One more thing you have to auto load lib file like:

config.autoload_paths << Rails.root.join('lib')

put this line at config/application.rb

Upvotes: 1

Pravesh Khatri
Pravesh Khatri

Reputation: 2244

when gets error it moves from http://localhost:3000/users/sign_up to http://localhost:3000/users with errors and form.

If you looked at the controllers in https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb

In registrations_controller.rb

Locate create method

 def create
    [..................]
       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)
    [..................]

You can easily see that when there are errors it will respond to resource, in your case it is users.

There are two options

  1. Either you override registrations controller and add redirection in the above lines.
  2. OR you can create strategy for Devise.

Upvotes: 0

Related Questions