组织多态资源的部分内容

发布于 2024-09-13 09:11:47 字数 702 浏览 3 评论 0原文

我正在寻找其他人通常如何组织多态资源的部分内容。

示例:

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

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

发布评论

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

评论(4

少年亿悲伤 2024-09-20 09:11:47

现在,在您的代码中,尚不清楚 _product 实际上是 _image

我认为 Rails 的目的是这样做:

shared/
    _image.html.haml
    imageable/
      _product.html.haml
      _post.html.haml
      _user.html.haml

然后在 _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:

shared/
    _image.html.haml
    imageable/
      _product.html.haml
      _post.html.haml
      _user.html.haml

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 an images_controller the directory should be called images)

情绪少女 2024-09-20 09:11:47

我认为您的方法是正确的,但我认为清洁的最后一步是消除选择另一个部分的图像部分,而是将该行为传递给助手。像...之类的东西

module ImagesHelper
  def show_image(imageable)
    render :partial => "images/#{imageable.class.to_s.tableize}"
  end  
end

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

module ImagesHelper
  def show_image(imageable)
    render :partial => "images/#{imageable.class.to_s.tableize}"
  end  
end

?

遗失的美好 2024-09-20 09:11:47

我有点不舒服,所以无法测试。但您应该输入 <%= 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.

梦巷 2024-09-20 09:11:47

我认为你最初的想法是正确的,只需稍作调整:

= render imageable.images

# images/_image.html.haml
= render image.imageable_type.underscore, :object => image.imageable, :image => image

# images/_product.html.haml
// Custom code for product image (with product and image local variables)

I reckon your original idea is on the right track, with a minor adjustment:

= render imageable.images

# images/_image.html.haml
= render image.imageable_type.underscore, :object => image.imageable, :image => image

# images/_product.html.haml
// Custom code for product image (with product and image local variables)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文