コントローラーを DRY するために inherited_resources を使用していますが、特定のコントローラーを正しく動作させる方法がわかりません。私のモデルでは、User has_one Person
. オプションでネストされ、ネストされている場合はシングルトンとして動作し、ネストされていない場合は非シングルトンとして動作するようにします。つまり、すべての既知の人 (/people) を一覧表示し、5 番目の人 (/person/5) を取得し、ユーザー 10 の唯一の人 (/user/10/person) を取得できるようにしたいと考えています。routes.rb の以下:
resources :users
resource :person
end
resources :people
...期待どおりにルートを設定します:
user_person POST /users/:user_id/person(.:format) people#create
new_user_person GET /users/:user_id/person/new(.:format) people#new
edit_user_person GET /users/:user_id/person/edit(.:format) people#edit
GET /users/:user_id/person(.:format) people#show
PUT /users/:user_id/person(.:format) people#update
DELETE /users/:user_id/person(.:format) people#destroy
people GET /people(.:format) people#index
POST /people(.:format) people#create
new_person GET /people/new(.:format) people#new
edit_person GET /people/:id/edit(.:format) people#edit
person GET /people/:id(.:format) people#show
PUT /people/:id(.:format) people#update
DELETE /people/:id(.:format) people#destroy
... とても素晴らしい。さて、PeopleController の場合は、次を使用します。
belongs_to :user, :optional => true
...次に、ネストされていない /people URL は機能しますが、ネストされた /users/:user_id/person URL は機能しません:undefined method 'people'
代わりに、PeopleController で次を使用する場合:
belongs_to :user, :optional => true, :singleton => true
... ネストされた /users/:user_id/person URL は機能しますが、ネストされていない場合でもシングルトンとして扱われるため、ネストされていない /people URL は機能しません。undefined method 'person'
概要: ネストされたルート経由でアクセスした場合は inherited_resources がリソースをシングルトンとして処理し、ネストされていないルート経由でアクセスした場合は非シングルトンとして処理する方法はありますか?