我正在尝试创建子资源不存在的嵌套资源。例如,Address
链接到Person
.
我的路线声明如下所示:
map.resources :persons,
:has_many => :addresses
这给了我以下路线:
person_addresses GET /persons/:person_id/addresses {:controller=>"addresses", :action=>"index"}
formatted_person_addresses GET /persons/:person_id/addresses.:format {:controller=>"addresses", :action=>"index"}
POST /persons/:person_id/addresses {:controller=>"addresses", :action=>"create"}
POST /persons/:person_id/addresses.:format {:controller=>"addresses", :action=>"create"}
new_person_address GET /persons/:person_id/addresses/new {:controller=>"addresses", :action=>"new"}
formatted_new_person_address GET /persons/:person_id/addresses/new.:format {:controller=>"addresses", :action=>"new"}
edit_person_address GET /persons/:person_id/addresses/:id/edit {:controller=>"addresses", :action=>"edit"}
formatted_edit_person_address GET /persons/:person_id/addresses/:id/edit.:format {:controller=>"addresses", :action=>"edit"}
person_address GET /persons/:person_id/addresses/:id {:controller=>"addresses", :action=>"show"}
formatted_person_address GET /persons/:person_id/addresses/:id.:format {:controller=>"addresses", :action=>"show"}
PUT /persons/:person_id/addresses/:id {:controller=>"addresses", :action=>"update"}
PUT /persons/:person_id/addresses/:id.:format {:controller=>"addresses", :action=>"update"}
DELETE /persons/:person_id/addresses/:id {:controller=>"addresses", :action=>"destroy"}
DELETE /persons/:person_id/addresses/:id.:format {:controller=>"addresses", :action=>"destroy"}
现在我想确保一个人的地址是只读的,即我只想要一个地址的show
andindex
操作。
我试过这个:
map.resources :persons,
:has_many => :addresses,
:collection => { :addresses => [ :show, :index ] }
并得到这个错误:
(in /home/rmk/app)
rake aborted!
Invalid HTTP method specified in route conditions: {:method=>:show}
(See full trace by running task with --trace)
有什么办法可以做到这一点?
编辑:我的主要问题是我没有正确使用代码块(我想尽可能使用 has_one 等)。所以我的persons
路线看起来像这样(有点人为的例子):
map.resources :persons, :has_one => :ssn, :except => [:new, :edit] do |person|
person.resources :addresses, :only => [:index, :show]
end