Rails 3-嵌套的资源和多态路径:可以分为两个级别,但可以分为三个级别 [英] Rails 3 - Nested resources and polymorphic paths: OK to two levels, but break at three

查看:85
本文介绍了Rails 3-嵌套的资源和多态路径:可以分为两个级别,但可以分为三个级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的家庭聚会网站:帖子",家庭",孩子"和图片".理想情况下,我希望这样构造路线/关系:

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

在模型中,我在famskids之间设置了必要的"belongs_to"和"has_many"关系. Famskidsposts都用"has_many :pictures, :as => :imageable定义,而图片定义为:belongs_to :imageable, :polymorphic => true

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

pictures视图中尝试执行link_to "Edit"link_to "Destroy"时,遇到了各种_path问题. polymoric_path在两个级别上都可以正常工作,即对于posts-picturesfams-pictures,但是它无法处理fams-kids-pictures的三个级别的情况.我猜想它不是设计来处理picture对象上方的两个级别的"imageable"对象.另一个问题是,在pictures控制器中,一个实例必须处理一级"资源嵌套的情况,而在另一个实例中,它必须处理二级"资源的情况.不确定如何解决这个问题.

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.

我尝试过的一件事是按照Ruby Guides的指导不要将资源嵌套超过一个深度.我这样构造它们:

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

由于不再保留家族与孩子之间的亲戚关系,这导致了另一组路径问题.我也无法使polymorphic_path在所有不同的picture视图中正常运行.

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.

所以这是我的主要问题:有谁知道Rails 3的示例/教程中嵌套资源,belongs-to/has_many和多态关系都被放在一起,尤其是在不仅仅是简单的两层结构的地方大多数例子显示的关系? (由于我缺乏Rails的历史经验,我对Rails和Rails 2在这些领域中发现的示例还很陌生.)

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.)

或者有人可以告诉我如何为我的picture视图构造link_to EDITlink_to DELETE语句以及如何为我的createupdatedestroy方法构造redirect-to语句在我的pictures控制器中?

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?

谢谢!

推荐答案

您的将示例嵌套限制为2级的代码示例非常接近答案.为避免fams-> kids和kids出现重复路线,可以将:only选项与空白数组一起使用,以使1级孩子不会生成路线,除非在kids->图片的情况下,例如:

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)

这篇关于Rails 3-嵌套的资源和多态路径:可以分为两个级别,但可以分为三个级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆