Rails 4 How Overwrite Devise Respond Path Upon Error

我是研究僧i 提交于 2020-01-01 04:14:06

问题


I've been struggling for a while with this one. I have a Rails4/Devise 3.1 app with two users in the system:

  1. Graduates
  2. Employers

and one devise User who can be either a Graduate or Employer via a polymorphic :profile association. I have Graduates sign up via /graduate/sign_up path and employers via /employer/sign_up path both of which route to the same /views/devise/registrations/new.html.erb view (since their signup form is pretty much the same - email and password). Everything works fine, except when there is a validation error, RegistrationsController#create.respond_with resource always redirects both user types to /users path and I need to redirect them back to where they originally came from, e.g. /graduate/sign_up or /employer/sign_up respectively. I've tried replacing respond_with with redirect_to, but I end up loosing the resource object with associated error messages. Any idea guys on how this can be done?

These are my models:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  belongs_to :profile, polymorphic: true
end

class Graduate < ActiveRecord::Base
  has_one :user, as: :profile 
end

class Employer < ActiveRecord::Base
  has_one :user, as: :profile
end

Registrations controller:

class RegistrationsController < Devise::RegistrationsController

  def create
    build_resource sign_up_params

    user_type = params[:user][:user_type]
    # This gets set to either 'Graduate' or 'Employer'
    child_class_name = user_type.downcase.camelize
    resource.profile = child_class_name.constantize.new

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_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_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords(resource)
      respond_with resource
    end
  end
end

Registration view (same for both, graduates and employers):

<h2>Sign up</h2>

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

  <div><%= f.label :email %><br />
  <%= f.email_field :email, :autofocus => true %></div>

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></div>
  <%= hidden_field resource_name, :user_type, value: params[:user][:user_type] %>

  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "devise/shared/links" %>

These are my routes:

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

 devise_scope :user do
    match 'graduate/sign_up', to: 'registrations#new', user: { user_type: 'graduate' }, via: [:get]
    match 'employer/sign_up', to: 'registrations#new', user: { user_type: 'employer' }, via: [:get]
  end

  root to: 'home#index'

Output of rake routes;

$ rake routes
                  Prefix Verb     URI Pattern                    Controller#Action
        new_user_session GET      /users/sign_in(.:format)       devise/sessions#new
            user_session POST     /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE   /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST     /users/password(.:format)      devise/passwords#create
       new_user_password GET      /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET      /users/password/edit(.:format) devise/passwords#edit
                         PATCH    /users/password(.:format)      devise/passwords#update
                         PUT      /users/password(.:format)      devise/passwords#update
cancel_user_registration GET      /users/cancel(.:format)        registrations#cancel
       user_registration POST     /users(.:format)               registrations#create
   new_user_registration GET      /users/sign_up(.:format)       registrations#new
  edit_user_registration GET      /users/edit(.:format)          registrations#edit
                         PATCH    /users(.:format)               registrations#update
                         PUT      /users(.:format)               registrations#update
                         DELETE   /users(.:format)               registrations#destroy
        graduate_sign_up GET /graduate/sign_up(.:format)    registrations#new {:user=>{:user_type=>"graduate"}}
        employer_sign_up GET /employer/sign_up(.:format)    registrations#new {:user=>{:user_type=>"employer"}}
                    root GET      /   

回答1:


Apparently, the :location parameter is ignored if you have an existing template called, well, in this case, #index. I still don't understand why Devise redirects to #index or #show when the resource has errors on it.

I'll update this answer as I find out more.




回答2:


Calling respond_with will render the default action of that resource. This I believe is the index action (/users/) and thus the cause for the redirect.

I think what you want to do is render the new action instead. Try the following:

if resource.save
  ...
else
  clean_up_passwords(resource)
  render :action => 'new'
end



回答3:


In case of respond_with, for an html response - if the request method is get, an exception is raised but for other requests such as post the response depends on whether the resource has any validation errors (i.e. assuming that an attempt has been made to save the resource, e.g. by a create action) -

If there are no errors, i.e. the resource was saved successfully, the response redirect's to the resource i.e. its show action.

If there are validation errors, the response renders a default action, which is :new for a post request or :edit for patch or put.

source: http://apidock.com/rails/v4.1.8/ActionController/MimeResponds/respond_with

For your case, something like render 'view_path' in place of respond_with block should work.



来源:https://stackoverflow.com/questions/19574643/rails-4-how-overwrite-devise-respond-path-upon-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!