2013-10-24 8 views
14

私はこれでしばらくの間苦労してきました。Rails 4上書きのデバッグ方法のエラー時の応答パス

  1. 卒業生
  2. 雇用

と多型:profile会合を介して大学院や雇用のいずれかになります1つの工夫ユーザー:私は、システムに2人のユーザーとRails4 /工夫3.1アプリを持っています。私は卒業生が/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 

登録コントローラ:

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 

登録ビュー(どちらも同じ、卒業生や雇用):

<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 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 / 
+0

登録ビューでurl:valueを置き換えた場合、私は気づきました。 '<%= form_for(リソース、as:リソース名、url:registration_path(resource_name))do | f | %> '%= form_for(resource、url:" sign_up ")do | f |%>' と %> ' それが動作します。しかし、これは正しい方法ではありません。 – demisx

+0

+1ここで私はこの問題を抱えており、ソースコードのどこにこのことが起こっているのか理解できません。 –

+0

私は同じ問題を抱えています、最後に、あなたは問題を解決しましたか? – user1066183

答えて

6

Apparentlyあなたは、よく、この場合、#INDEXと呼ばれる既存のテンプレートを持っている場合、:locationパラメータは無視されます。私はまだリソースがエラーを持っているときにDeviseが#indexまたは#showにリダイレクトする理由を理解できません。

詳細については、この回答を更新します。

3

respond_withを呼び出すと、そのリソースのデフォルト動作がレンダリングされます。これは私が考えているのはindexアクション(/ users /)であり、リダイレクトの原因です。

あなたがしたいことは、代わりにnewアクションをレンダリングすると思います。次のことを試してみてください。

respond_withの場合
if resource.save 
    ... 
else 
    clean_up_passwords(resource) 
    render :action => 'new' 
end 
0

、HTML応答のために - リクエストメソッドがGETの場合、例外が発生しますが、そのようなポストなど他の要求に対する応答は、リソースがいずれかを持っているかどうかに依存します(例えば、アクションを作成してリソースを保存しようとしたと仮定した場合) -

エラーがない場合、つまりリソー​​スが正常に保存された場合、 レスポンスリダイレクトはリソースアクション。

検証エラーがある場合、レスポンスは既定のアクション をレンダリングします。これは、ポストリクエストの場合はnew、patchまたはputの場合はeditです。

ソース:あなたのケースのためにhttp://apidock.com/rails/v4.1.8/ActionController/MimeResponds/respond_with

respond_withブロックの代わりにrender 'view_path'ようなものが動作するはずです。

関連する問題