限制 Rails 中嵌套资源的路由
我正在尝试创建嵌套资源,其中子资源不存在自己的存在。例如,链接到Person
的Address
资源。
我的路线声明如下所示:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是你想做的吗?
这将创建地址作为人员的嵌套资源,但仅使索引和显示视图可用。
Is this what you're trying to do?
This creates addresses as a nested resource of persons, but only makes the index and show views available.