限制 Rails 中嵌套资源的路由

发布于 2024-09-09 07:51:47 字数 2847 浏览 2 评论 0原文

我正在尝试创建嵌套资源,其中子资源不存在自己的存在。例如,链接到PersonAddress 资源。

我的路线声明如下所示:

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"}

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

我尝试了这个:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不乱于心 2024-09-16 07:51:47

这就是你想做的吗?

map.resources :persons 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文