组织多态资源的部分内容
我正在寻找其他人通常如何组织多态资源的部分内容。
示例:
我有 Image
,它是多态的,并且根据 imageable
的内容,我想显示稍微不同的内容。
我有一个部分images/_image
并且可以调用render imageable.images
。我当前的想法是让我的图像部分检查 imageable
的类型,然后针对该情况设置另一个部分。我的组织将类似于:
images/
_image.html.haml
_product.html.haml
_post.html.haml
_user.html.haml
我的 _image
部分看起来像:
%div
= render :partial => "images/#{imageable.type}"
这看起来是一个糟糕的方法,还是完全错误的方法?我认为从任何地方调用 render imageable.images 都会比调用 render :partial => 要好得多。 ...到处都是。
任何想法将不胜感激。你是怎么做到的?
编辑:很长一段时间过去了,我仍然想知道是否有人对此有任何意见。悬赏赏金看看是否会引起一些注意。
I'm looking for how others typically organize their partials for a polymorphic resource.
Example:
I have Image
which is polymorphic and depending on what is imageable
, I want to display things slightly different.
I have a partial images/_image
and can call render imageable.images
. My current mindset is to have my image partial check what type imageable
is and then have another partial, specific to that case. My organization would be something along the lines of:
images/
_image.html.haml
_product.html.haml
_post.html.haml
_user.html.haml
My _image
partial would look something like:
%div
= render :partial => "images/#{imageable.type}"
Does this seem like a bad approach, or flat out the wrong approach? I think it would be much nicer to just call render imageable.images
from anywhere than having to call render :partial => ...
all over the place.
Any ideas would be greatly appreciated. How have you done it?
EDIT: A long time has gone by and I'm still wondering if anyone has any input on this. Throwing up a bounty to see if that draws some attention.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
现在,在您的代码中,尚不清楚
_product
实际上是_image
。我认为 Rails 的目的是这样做:
然后在
_image
部分中调用:render :partial =>; imageable/#{image.type}", :image => image
现在从你的目录结构中可以清楚地看出你的多态性是什么。
(注意:我使用了
shared
目录,但是当然,当您的图像部分实际上并未在视图之间共享时,它应该位于某个视图目录中,就像您实际上有一个images_controller
一样,该目录应该称为images
)In your code now it's unclear that
_product
actually is an_image
.I think the intention in Rails is to do it like this:
Then in the
_image
partial you call:render :partial => imageable/#{image.type}", :image => image
Now it is clear from your directory structure what your polymorphism is.
(note: I used the
shared
directory, but ofcourse when your image partial isn't actually shared between views it should be in some view directory like if you actually have animages_controller
the directory should be calledimages
)我认为您的方法是正确的,但我认为清洁的最后一步是消除选择另一个部分的图像部分,而是将该行为传递给助手。像...之类的东西
?
I think you've got the right approach, but i think the final step for cleanliness is to eliminate the images partial that selects another partial by instead passign that behavior to a helper. something like...
?
我有点不舒服,所以无法测试。但您应该输入
<%= render imageable.images %>
并查看其内容。它应该可以工作,因为它应该使用 .modelname 或 .class :) 如果它不起作用,我会按照上面提到的那样做你正在做的事情。如果它不起作用,请评论它所说的内容,因为那时我可能会为此对 Rails 核心进行修补,确实应该像这样工作。I'm a bit under the weather so I can't test it. But you should just put in
<%= render imageable.images %>
and see what it says. It should work because it should be using .modelname or .class :) If it doesn't work I would do what you're doing as mentioned above. If it doesn't work though, please comment with what it says, because then I'll probably make a patch to the rails core for that, really should work as such.我认为你最初的想法是正确的,只需稍作调整:
I reckon your original idea is on the right track, with a minor adjustment: