无法访问多态关联模型的属性

时间:2012-01-26 18:11:18

标签: ruby-on-rails activerecord ruby-on-rails-3.1 polymorphism polymorphic-associations

我有一些使用Photo多态模型的模型。通过控制台或检查或记录器保存或访问照片属性一切都很好,但如果我尝试直接在视图中调用它们,例如@banner.photo.file,它会返回undefined method 'file' etc for nil:Nil

通过回形针上传照片,所有模型都设置为has_one :photo, :as => :imageableaccepts_nested_attributes_for :photo, :allow_destroy => true。照片模型设置为belongs_to :imageable, :polymorphic => truehas_attached_file :file

这些是Photo模型的列:

id:integer
file:string
imageable_id:integer
imageable_type:string
file_file_name:string
file_content_type:string
file_file_size:integer

(除了created_at等)

在我看来:

<% @banners.each do |b| %> 
  <%= b.inspect %><br /> 
  <%= b.photo.inspect %><br /><br /> 
<% end %>

您可以在http://bdyvu2.hospedagemweb.net/

看到检查

非常感谢提前!!

0 个答案:

没有答案