I have a few models that use a Photo polymorphic model. It’s all good
and fine for saving or accessing the photos’ attributes via console or
inspect or logger, but if I try to call them directly in the view
with, for example, @banner.photo.file, it returns a undefined method
‘file’ etc for nil:Nil
Photos are being uploaded via paperclip and all models are set with
has_one :photo, :as => :imageable and
accepts_nested_attributes_for :photo, :allow_destroy => true. And the
Photo model is set as belongs_to :imageable, :polymorphic => true and
has_attached_file :file.
I have a few models that use a Photo polymorphic model. It’s all good
and fine for saving or accessing the photos’ attributes via console or
inspect or logger, but if I try to call them directly in the view
with, for example, @banner.photo.file, it returns a undefined method
‘file’ etc for nil:Nil
That means that @banner.photo is nil. Add code to your view to test @banner.photo before accessing @banner.photo.file.
oh hi!
I did… I can see it if I inspect @banner.photo, but if I try to
access it directly (using any of the name variations), it doesn’t work
for ANY of the attributes
Please quote the previous message when replying and insert your reply
into the previous message so that we don’t have to find the previous
mail to know what you are talking about. Thanks.
You say that @banner.photo works but accessing it directly does not,
what do you mean by that? You could copy and paste the console output
showing the inspect and explain what you mean by “doesn’t work”.
Colin
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.