0

I have the sign-up and sign-in form on the same page and want any sign-up and sign-in errors such as 'email already exists' to appear on this page. I've customised the Registrations controller using this solution and also created a custom DeviseFailure class so errors on sign in redirect back to this page.

I have 2 questions:

  1. If the sign up fails due to an error in the form (e.g. entering an email address that has already been registered) and then I try to sign in I get an

    Internal Server Error - inGET, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH.

If I then reload the redirect occurs correctly. I dont know how to address this error.

  1. How do I trap errors just for the sign in form or just for the sign up form? Currently the errors are displaying twice - once for each form.

routes.rb

  authenticated :user do
    root 'preorders#by_campaign', as: :authenticated_root
  end

  root to: 'users#index'

  devise_for :users, :controllers => { :sessions => "sessions",
                                   :passwords => "passwords",
                                   :registrations => "registrations" }

application_controller.rb

def after_sign_in_path_for(resource)
  preorders_by_campaign_url
end

def after_sign_out_path_for(resource)
  root_url
end

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    after_sign_in_path_for(resource)
  end

  def create
    build_resource(sign_up_params)

    if resource.save
      yield resource if block_given?
      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
      resource.errors.full_messages.each {|x| flash[x] = x} # Rails 4 simple way
      redirect_to root_path 
    end
  end
end

index.rb

.user_form
  %h2 Sign up

  = form_for(resource, :as => resource_name, :url =>  registration_path(resource_name)) do |f| 
  = render partial: "errors", locals: {flash: flash}

    %div
      = f.label :email 
      = f.email_field :email

    %div
      = f.label :password 
      = f.password_field :password

    %div
      = f.label :password_confirmation 
      = f.password_field :password_confirmation

    %div= f.submit "Sign up"

 .user_form
   %p Already signed up? Sign in

   = form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| 
     = render partial: "errors", locals: {flash: flash}
     %div
      = f.label :email 
      = f.email_field :email

     %div
       = f.label :password 
       = f.password_field :password

     %div= f.submit "Sign in"

_errors.html.haml

-if flash.any?
  %ul{class: "errors"}
  - flash.each do |msg|
    %li=msg.first
Community
  • 1
  • 1
margo
  • 2,927
  • 1
  • 14
  • 31
  • Please post your form template and the method definitions for `after_sign_up_path_for` and `after_inactive_sign_up_path_for`. It seems likely that the code in those places will be relevant to your problem. – RickyTomatoes Feb 05 '16 at 16:50
  • I dont have an after_sign_up_path_for or an after_inactive_sign_up_path_for. I use authenticated_root in the routes to redirect the user. I have added the form views. – margo Feb 05 '16 at 17:02
  • I do have an after_sign_in_path_for method in the application controller as well as an after_sign_out_path, Ive now shown that code – margo Feb 05 '16 at 17:31

0 Answers0