Rails 3 - 嵌套资源和多态路径:可以达到两级,但在三级时中断

发布于 2024-11-05 18:11:28 字数 1731 浏览 4 评论 0 原文

我正在尝试做一个简单的家庭聚会网站,其中包含:“帖子”、“家庭”、“孩子”和“图片”。理想情况下,我希望路由/关系的结构如下:

  resources :posts do
    resources :pictures
  end

  resources :fams do
     resources :pictures
     resources :kids do
       resources :pictures
     end
  end

在模型中,我在 fams 之间设置了必要的“belongs_to”和“has_many”关系 和孩子Famskidsposts 均使用 "has_many :pictures, :as => :imageable 定义" 而图片定义为:belongs_to :imageable, :polymorphic => true

当尝试在 pictures 视图中执行 link_to "Edit"link_to "Destroy" 时,我遇到了各种< code>_path 问题。 polymoric_path 在两个级别上工作正常,即对于 posts-picturesfams-pictures 但它无法处理 的三级情况fams-kids-图片。我猜测它的设计目的不是为了处理 picture 对象上方的两层“imageable”对象。另一个问题是,在一种情况下,图片控制器必须处理“一级”资源嵌套情况,而在另一种情况下,它必须处理“两级”情况。不知道如何解决这个问题。

我尝试过的一件事是,根据 Ruby Guides 的说明,将资源嵌套的深度不要超过一层。我这样构造它们:

  resources :posts do
    resources :pictures
  end

  resources :fams do
     resources :pictures
     resources :kids
  end

  resources :kids do
     resources :pictures
  end

这导致了另一组路径问题,因为家庭与孩子的关系不再保留。我也无法让 polymorphic_path 在所有不同的 picture 视图中正确运行。

所以这是我的主要问题:有谁知道 Rails 3 示例/教程,其中嵌套资源、belongs-to/has_many 和多态关系都放在一起,特别是它不仅仅是最简单的两级关系。例子表明? (我对 Rails 相当陌生,鉴于我缺乏 Rails 历史经验,我在这些领域发现的 Rails 2 示例令人困惑。)

或者有人可以告诉我如何构建 link_to EDIT 和我的 picture 视图的 link_to DELETE 语句,以及我的 createredirect-to 语句,pictures 控制器中的 >update 和 destroy 方法?

谢谢!

I'm trying to do a simple family reunion site with: "posts", "families", "kids", and "pictures". Ideally I'd like the routes/relationships to be structured this way:

  resources :posts do
    resources :pictures
  end

  resources :fams do
     resources :pictures
     resources :kids do
       resources :pictures
     end
  end

In the models I have the necessary "belongs_to" and "has_many" relationships set between fams and kids. Fams, kids, and posts all are defined with "has_many :pictures, :as => :imageable" while pictures are defined as: belongs_to :imageable, :polymorphic => true

When trying to do link_to "Edit" and link_to "Destroy" in the pictures views I run into all sorts of _path problems. polymoric_path works fine at two levels, namely for posts-pictures and fams-pictures but it fails to handle the three level case of fams-kids-pictures. I'm guessing that it was not designed to handle the two levels of "imageable" objects above the picture object. Another issue is that in one instance the pictures controller has to handle a "one level" resource-nesting situation and in another it has to handle a "two levels" situation. Not sure how to approach this.

One thing I did try was to not nest resources more than one deep, per the Ruby Guides directions. I structured them like this:

  resources :posts do
    resources :pictures
  end

  resources :fams do
     resources :pictures
     resources :kids
  end

  resources :kids do
     resources :pictures
  end

This caused another set of problems with paths since the fam to kid relationship was no longer preserved. I also could not get polymorphic_path to function correctly accross all the different picture views.

So here is my main question: Does anyone know of a Rails 3 example/tutorial where nested resources, belongs-to/has_many, and polymorphic relationships are all put together, especially where it is not just the simple, two-level relationship that most examples show? (I'm fairly new to Rails and the Rails 2 examples I've found in these areas are confusing given my lack of Rails historical experience.)

Or can someone tell me how to structure the link_to EDIT and link_to DELETE statements for my picture views, as well as the redirect-to statement for my create, update, and destroy methods in my pictures controller?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

谈场末日恋爱 2024-11-12 18:11:28

将嵌套限制为 2 层的代码示例非常接近答案。为了避免 fams->kids 和 kids 的重复路由,您可以使用带有空白数组的 :only 选项,以便第一级 kids 不会生成路由,除非在 kids->pictures 的上下文中,如下所示

resources :posts do
  resources :pictures
end

resources :fams do
  resources :pictures
  resources :kids
end

resources :kids, only: [] do # this will not generate kids routes
   resources :pictures
end

:上面的代码,您可以使用以下内容来构造您的多态编辑 url:

polymorphic_url([fam, picture], action: :edit) # using Ruby 1.9 hash syntax
polymorphic_url([kid, picture], action: :edit)

Your code example that limited your nesting to 2 levels is quite near the answer. To avoid duplicate routes for fams->kids and kids, you can use the :only option with a blank array so that the 1st-level kids will not generate routes except in the context of kids->pictures, like so:

resources :posts do
  resources :pictures
end

resources :fams do
  resources :pictures
  resources :kids
end

resources :kids, only: [] do # this will not generate kids routes
   resources :pictures
end

For the above code, you can use the following to construct your polymorphic edit url:

polymorphic_url([fam, picture], action: :edit) # using Ruby 1.9 hash syntax
polymorphic_url([kid, picture], action: :edit)
吃素的狼 2024-11-12 18:11:28

一段时间以来一直遇到同样的问题。我现在可以使用它,但它并不漂亮:S

来自嵌套的怪物,例如:

http://localhost:3000/destinations/3/accommodations/3/accommodation_facilities/52

您的 params 对象最终看起来像这样:

action: show
id: "52"
destination_id: "3"
accommodation_id: "3"
controller: accommodation_facilities

其中“id”代表当前模型 ID(链上的最后一个) )和其他具有 model_name_id

要正确呈现此页面上的另一个嵌套链接,您需要传入组成完整路径的对象数组,例如链接到您必须执行的虚构 FacilityType 对象

 <%= link_to "New", new_polymorphic_path([@destination, @accommodation, @accommodation_facility, :accommodation_facility_type]) %>

:这个数组来自 params 对象,我在 application_helper.rb 中使用此代码

def find_parent_models(current_model = nil)
    parents = Array.new

    params.each do |name, value|
      if name =~ /(.+)_id$/
        parents.push $1.classify.constantize.find(value)
      end
    end

    parents.push current_model

    parents
end

然后自动创建相同的链接,您可以愉快地做:

 <%= link_to "New", new_polymorphic_path(find_parent_models(@accommodation_facility).push(:accommodation_facility_type)) %>

非常欢迎任何关于使此解决方案不那么粗略的指示:]

Have been having this exact same problem for a while. I have it working now, but it isn't beautiful :S

From a nested monster like:

http://localhost:3000/destinations/3/accommodations/3/accommodation_facilities/52

Your params object ends up looking like this:

action: show
id: "52"
destination_id: "3"
accommodation_id: "3"
controller: accommodation_facilities

where "id" represents the current model id (last on the chain) and the other ones have model_name_id

To correctly render another nested link on this page, you need to pass in an array of objects that make up the full path, eg to link to a fictional FacilityType object you'd have to do:

 <%= link_to "New", new_polymorphic_path([@destination, @accommodation, @accommodation_facility, :accommodation_facility_type]) %>

To generate this array from the params object, I use this code in application_helper.rb

def find_parent_models(current_model = nil)
    parents = Array.new

    params.each do |name, value|
      if name =~ /(.+)_id$/
        parents.push $1.classify.constantize.find(value)
      end
    end

    parents.push current_model

    parents
end

Then to automatically make the same link, you can merrily do:

 <%= link_to "New", new_polymorphic_path(find_parent_models(@accommodation_facility).push(:accommodation_facility_type)) %>

Any pointers on making this solution less sketchy are very welcome :]

温柔少女心 2024-11-12 18:11:28

我不能谈论多态关联问题(可能需要有关实际错误的更多信息),但通过仅定义一层深度的嵌套资源,您确实朝着正确的方向前进。这是 Jamis Buck 撰写的热门文章,已成为参考,您可以参考也许应该检查一下。

I can't speak for the polymorphic association problem (probably need more info on the actual error) but you are indeed headed in the right direction by defining your nested resources only one level deep. Here's a popular article by Jamis Buck that has become a reference and that you should probably check out.

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