【问题标题】:Access polymorphic attribute in the view访问视图中的多态属性
【发布时间】:2013-08-10 00:25:09
【问题描述】:

试图解决这个小问题,我终于让我的多态关联工作了,但现在似乎无法弄清楚如何在保存记录后在视图中显示图像。

我的设置

class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true

attr_accessible :photo
has_attached_file :photo, :styles => { :small_blog => "250x250#", :large_blog => "680x224#", :thumb => "95x95#" }
end

class Post < ActiveRecord::Base

has_many :images, as: :imageable

accepts_nested_attributes_for :images
attr_accessible :comments, :title, :images_attributes

end

所以在我看来,我现在有这个,它抛出未定义的方法“照片”错误

<% @posts.each do |p| %>
<%= image_tag(p.photo.url(:large_blog), :class => 'image') %>
<% end %>

欢迎指点

【问题讨论】:

  • 对不起,如果我遗漏了一些东西,但你在帖子上的照片,但根据模特帖子有很多图片,每张图片都有一张照片,而不是帖子。
  • 你会如何处理这个问题?也许这就是为什么我搞混了?感谢任何指针

标签: ruby ruby-on-rails-3 associations paperclip


【解决方案1】:

帖子有很多图片,每张图片都有一张照片。所以它可以是这样的:

<% @posts.each do |p| %> 
  <% p.images.each do |i| %>
    <%= image_tag(i.photo.url(:large_blog), :class => 'image') %>
  <% end %>
<% end %>

【讨论】:

  • 啊,我明白了,是的,现在说得通了..谢谢你把它清理干净,在处理视图中的嵌套属性时总是搞混..所以这个帖子有很多图片,所以我们按照路径通过块,发布 -> 图像 -> 属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 2012-03-04
相关资源
最近更新 更多