我正在尝试做一个简单的家庭聚会网站,其中包含:“帖子”、“家庭”、“孩子”和“图片”。理想情况下,我希望路由/关系的结构如下:
resources :posts do
resources :pictures
end
resources :fams do
resources :pictures
resources :kids do
resources :pictures
end
end
在模型中,我在 fams 之间设置了必要的“belongs_to
”和“has_many
”关系 和孩子
。 Fams
、kids
和 posts
均使用 "has_many :pictures, :as => :imageable
定义" 而图片定义为:belongs_to :imageable, :polymorphic => true
当尝试在 pictures
视图中执行 link_to "Edit"
和 link_to "Destroy"
时,我遇到了各种< code>_path 问题。 polymoric_path
在两个级别上工作正常,即对于 posts-pictures
和 fams-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
语句,以及我的 create
的 redirect-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!
发布评论
评论(3)
将嵌套限制为 2 层的代码示例非常接近答案。为了避免 fams->kids 和 kids 的重复路由,您可以使用带有空白数组的 :only 选项,以便第一级 kids 不会生成路由,除非在 kids->pictures 的上下文中,如下所示
:上面的代码,您可以使用以下内容来构造您的多态编辑 url:
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:
For the above code, you can use the following to construct your polymorphic edit url:
一段时间以来一直遇到同样的问题。我现在可以使用它,但它并不漂亮:S
来自嵌套的怪物,例如:
您的 params 对象最终看起来像这样:
其中“id”代表当前模型 ID(链上的最后一个) )和其他具有 model_name_id
要正确呈现此页面上的另一个嵌套链接,您需要传入组成完整路径的对象数组,例如链接到您必须执行的虚构 FacilityType 对象
:这个数组来自 params 对象,我在 application_helper.rb 中使用此代码
然后自动创建相同的链接,您可以愉快地做:
非常欢迎任何关于使此解决方案不那么粗略的指示:]
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:
Your params object ends up looking like this:
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:
To generate this array from the params object, I use this code in application_helper.rb
Then to automatically make the same link, you can merrily do:
Any pointers on making this solution less sketchy are very welcome :]
我不能谈论多态关联问题(可能需要有关实际错误的更多信息),但通过仅定义一层深度的嵌套资源,您确实朝着正确的方向前进。这是 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.