ruby-on-rails - Rails 4 如何覆盖设计响应路径出错

标签 ruby-on-rails devise

我一直在为这个而苦苦挣扎。我有一个 Rails4/Devise 3.1 应用程序,系统中有两个用户:

  • 毕业生
  • 雇主

  • 和一个设计用户,他可以通过多态 :profile 成为毕业生或雇主协会。我有毕业生通过 /graduate/sign_up 注册路径和雇主通过 /employer/sign_up两条路径都指向同一条 /views/devise/registrations/new.html.erb查看(因为他们的注册表单几乎相同 - 电子邮件和密码)。一切正常,除非出现验证错误,RegistrationsController#create.respond_with resource总是将两种用户类型重定向到 /users路径,我需要将它们重定向回它们最初来自的位置,例如/graduate/sign_up/employer/sign_up分别。我试过更换 respond_withredirect_to ,但我最终丢失了带有相关错误消息的资源对象。关于如何做到这一点的任何想法?

    这些是我的模型:
    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
    

    注册 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
    

    注册 View (毕业生和雇主都一样):
    <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" %>
    

    这些是我的路线:
     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'
    

    rake 路输出;
    $ 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      /   
    

    最佳答案

    Apparently , :location如果您有一个现有模板,则参数将被忽略,在本例中为 #index。我仍然不明白为什么当资源有错误时,Devise 会重定向到 #index 或 #show。

    当我了解更多信息时,我会更新这个答案。

    关于ruby-on-rails - Rails 4 如何覆盖设计响应路径出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19574643/

    相关文章:

    ruby-on-rails - 使用 FeedJira 创建 RSS 聚合器/阅读器

    ruby-on-rails - I18n 不使用公寓 gem rails 加载翻译

    ruby-on-rails - 渲染 :json without a template

    ruby-on-rails - 设计 + Omniauth + 推特。如何获取用户信息?

    ruby-on-rails - 设计销毁 session 不会销毁 session ?

    ruby-on-rails - 调用方法,在Application controller中定义,在其他controller中

    ruby-on-rails - 模型中某些 Action 的验证

    ruby-on-rails - 无法在 Rubymine 或 Vim for Rails 6 中保存和编辑 credentials.yml.enc

    ruby-on-rails - 使用 Devise 登录后没有任何反应

    ruby-on-rails - token 认证注销 : should I delete the token?