我正在尝试创建嵌套资源,其中子资源不存在自己的资源。例如,与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
和index
个地址操作。
我试过了:
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
答案 0 :(得分:2)
这是你想要做的吗?
map.resources :persons do |person|
person.resources :addresses, :only => [:index, :show]
end
这会将地址创建为人员的嵌套资源,但只会使索引和显示视图可用。