2013-06-27 14 views
10
  1. devise_forブロックにはいつルートをネストする必要がありますか?ユースケースを示すために1つまたは2つの例を挙げてください。 (ルート#1)Rails3 + Devise:devise_forとネストされたリソースにリソースをネストするとき

  2. :foo_object:usersので:user has_oneの:foo_objectに関連付けられている場合、私は:usersの下に巣:foo_objectに必要なのでしょうか? (ルート#2):usersは、機種が:usersである。

ルート#1:

devise_for :users 
resource :foo_object 

ルート#2:

devise_for :users 
resources :users do  
    resource :foo_object 
end 

答えて

23

次の例:

devise_for :users, :path => 'accounts' 

resources :users do 
    resources :orders 
end 

上記認証パスは"/accounts/sign_in"であろうことを意味します、がで実際にUsersControllerとモデルに対応していないことを認識することが重要であることを知っている人もいるかもしれません。それはそれがそれのように見える多くのbeleiveでも、そのリソースルートさえも。これは我々は次のように扱うことができない理由である。だから、あなたは次の操作を行うことができると言うに

# Session routes for Authenticatable (default) 
    new_user_session GET /users/sign_in     {:controller=>"devise/sessions", :action=>"new"} 
     user_session POST /users/sign_in     {:controller=>"devise/sessions", :action=>"create"} 
destroy_user_session GET /users/sign_out     {:controller=>"devise/sessions", :action=>"destroy"} 

# Password routes for Recoverable, if User model has :recoverable configured 
    new_user_password GET /users/password/new(.:format)  {:controller=>"devise/passwords", :action=>"new"} 
    edit_user_password GET /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"} 
     user_password PUT /users/password(.:format)   {:controller=>"devise/passwords", :action=>"update"} 
         POST /users/password(.:format)   {:controller=>"devise/passwords", :action=>"create"} 

# Confirmation routes for Confirmable, if User model has :confirmable configured 
new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"devise/confirmations", :action=>"new"} 
    user_confirmation GET /users/confirmation(.:format)  {:controller=>"devise/confirmations", :action=>"show"} 
         POST /users/confirmation(.:format)  {:controller=>"devise/confirmations", :action=>"create"} 

が、いくつかの競合を持っているでしょう:

devise_for :users do 
    resources: somereosouce 
end 

すべてdevise_forない次のルートをマップしています:ネストされたリソース上の

devise_for :users 

resource :users do 
    resource :foo_object 
end 

少しは、次のようなものがある場合:

class Users < ActiveRecord::Base 
    has_many :foo_object 
end 

class FooObject < ActiveRecord::Base 
    belongs_to :users 
end 

次に、あなたのネストされたリソースが

resource :users do 
    resource :foo_object 
    end 

うまくいけば、これは物事をクリアになります。また、Nested Resource with Devise - Rails3

+2

を読んでみてください。「devise_for」質問を明確にしてくれてありがとう。私が読んだベスト解説! – HM1

+0

Q#2のために、私はリソースを入れ子にすることを知っています...それは必要かどうか、またはこのケースのための関連モデルのリソースを入れなければならないのでしょうか?この場合、コントローラで 'current_user'を使用し、':foo_object'モデルをビルド/アップデートすることができます。私はこのようにすることに結果があるのだろうかと思っています。 – HM1

+1

@ HM1それは必須ではありませんが、アプリケーションのロジックを定義したい場合はyesです。あなたのリソースをグループ化する、よりクリーンな方法です。しかし、あなたの場合、 'user'は' foo_object'に関連付けられていることを知っています。そのため、あなたが 'user'へのアクションを実行すると' foo_object'アクションも実行されます。したがって、あなたのURLは '/ users/3/foo_object/4'のように見える可能性があります。ネストされたルートを持つ必要があるもう一つの理由は、RailsのRESTfulな1つの原則です。 – David