Adding a photo to a has_many model from a view
I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to manage everything. In the show view for Thing, I want to load a file and link it to the Thing model.
For some reason I am completely compressing how this should be done. I've tried doing this (Haml):
- form_for @thing.thing_photos, :html => {:multipart => true} do |f|
= f.file_field :photo
= f.submit
... and I am getting this error:
undefined method `array_path' for #<ActionView::Base:0x24d42b4>
Google is failing. I'm sure it's very simple, but I just can't get around his brain.
Edit: I should have mentioned that if I change @ thing.thing_photos to just @thing, it works great as it renders the form, but of course not associated with the correct model.
source to share
If you are not attached to Paperclip (and if you are just making images), I have had better luck with fleximage . It got really good documentation for image loading, manipulation and display (with all kinds of neat effects like watermarks and stuff).
It doesn't quite answer your question, but it might be a cool thing.
But I'm pretty sure your problem is related to the fact that you are creating a shape for an array of objects (via association). I bet something finds the object type (should be "thing_photo", but there is an "array" in your example) and adding a "path" to it to record the url of the form. Perhaps you should add .first
or create a partial?
source to share
The form must be for @thing_photo and must either pass in thing_id or if it is a nested route you can get it from this
then in your controller thing_photos
@thing_photo = Photo.new(params[:thing_photo])
#if using nested route
@thing = Thing.find(params[:thing_id])
#if using hidden field
@thing = Thing.find(params[:thing_photo][:thing_id])
if @thing_photo.save
#this is what ties the thing photo to the thing
@thing.thing_photos << @thing_photo
end
If I understood the question correctly, it might help.
source to share