I am trying to create nested resources where the sub-resource doesn't have an existence of its own. e.g., an Address
resource which is linked to a Person
.
My route declaration looks like so:
map.resources :persons,
:has_many => :addresses
This gives me the following routes:
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"}
Now I want to make sure that a person's addresses are read-only, i.e., I only want the show
and index
actions for an address.
I tried this:
map.resources :persons,
:has_many => :addresses,
:collection => { :addresses => [ :show, :index ] }
and got this error:
(in /home/rmk/app)
rake aborted!
Invalid HTTP method specified in route conditions: {:method=>:show}
(See full trace by running task with --trace)
Is there any way I can achieve this?
EDIT:
My main problem was that I was not using the code block properly (I wanted to use has_one etc. as much as possible). So my persons
routes look like this (somewhat contrived example):
map.resources :persons, :has_one => :ssn, :except => [:new, :edit] do |person|
person.resources :addresses, :only => [:index, :show]
end
Is this what you're trying to do?
map.resources :persons do |person|
person.resources :addresses, :only => [:index, :show]
end
This creates addresses as a nested resource of persons, but only makes the index and show views available.
精彩评论