私は2つのクラスを持っています:
class User < ActiveRecord::Base
:has_one :foo
end
class Foo < ActiveRecord::Base
:belongs_to :user
end
Foo はオプションです。
次のルーティングを作成しました。
resources :users do
resources :foo
end
その結果、次のルートになります。
GET /users/:user_id/foo(.:format) {:controller=>"foos", :action=>"index"}
user_foos POST /users/:user_id/foo(.:format) {:controller=>"foos", :action=>"create"}
new_user_foo GET /users/:user_id/foo/new(.:format) {:controller=>"foos", :action=>"new"}
GET /users/:user_id/foo/:id(.:format) {:controller=>"foos", :action=>"show"}
PUT /users/:user_id/foo/:id(.:format) {:controller=>"foos", :action=>"update"}
user_foo DELETE /users/:user_id/foo/:id(.:format) {:controller=>"foos", :action=>"destroy"}
edit_user_foo GET /users/:user_id/foo/:id/edit(.:format) {:controller=>"foos", :action=>"edit"}
質問:
- Index および Show アクションは冗長なようです。それらの1つを削除する必要がありますか?もしそうなら、どれですか?
- user_id は foos テーブルの外部キーであり、ユーザーごとに 1 つの foo しかないため、Show アクションの :id パラメータは不要のようです。私は間違っていますか?
- foo がない場合に New アクションにルーティングする適切な方法が必要です。1 つのオプションは、@user.foo.nil をテストすることでしょうか? FooController の Show または Index アクションで、次に New アクションにリダイレクトします。より良い方法はありますか?
御時間ありがとうございます。