2

我正在尝试创建子资源不存在的嵌套资源。例如,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"}

现在我想确保一个人的地址是只读的,即我只想要一个地址的showandindex操作。

我试过这个:

  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
4

1 回答 1

2

这是你想要做的吗?

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.

于 2010-07-14T21:00:06.550 回答