如何使用来自Pin视图的id访问Image.photo?
目前,我正在这样做,它给我随机数,如果我删除image_tag或银行形象空间。
<td><%= image_tag Image.find_by_id(pin.pic) %></td>这一次给出了一个未定义的方法: nil:NilClass的“照片”
<td><%= image_tag Image.find_by_id(pin.pic).photo.url(:small) %></td>
class Pin < ActiveRecord::Base
attr_accessible :board, :pic, :pin_time, :user, :apic, :photo
belongs_to :auser, :class_name => 'User', :foreign_key => 'user'
belongs_to :apic, :class_name => 'Image',:foreign_key => 'pic'
end
class Image < ActiveRecord::Base
attr_accessible :description, :uid, :web_url, :photo, :photo_file_name,:photo_content_type, :photo_file_size, :photo_updated_at, :id
has_attached_file :photo, :styles => { :small => "150x150>", :medium => "300x300", :large => "600x600"}
belongs_to :user
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
end发布于 2013-12-14 07:02:50
为什么不简单地
pin.apic.photo.url(:small)还是你在寻找其他完全的东西?
https://stackoverflow.com/questions/20579364
复制相似问题