views:

388

answers:

3

I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to manage everything. On the "show" view for Thing, I want to have a file upload, and have it relate to the Thing model.

For some reason, I'm totally glitching on 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 get this error:

undefined method `array_path' for #<ActionView::Base:0x24d42b4>

Google is failing me. I'm sure this is super easy, but I just can't get my brain around it.

Edit: I should have mentioned that if I change the @thing.thing_photos to just @thing, it works fine, in that it displays the form, but of course it's not associated with the correct model.

A: 

If you're not tied to Paperclip (and if you're just doing images), I've had a lot of luck with fleximage. It's got really good documentation for image upload, manipulation, and display (with all sorts of neat effects like watermarking and stuff).

It doesn't quite answer your question, but it might be a cool thing to look into.

But I bet your problem related to the fact you're creating a form for an array of objects (through an association). I bet something is finding the type of the object (should be "thing_photo" but is "array" in your example), and appending "path" to it to write the form post URL. Perhaps you should add a .first or create a partial?

zenazn
+1  A: 

try @thing.thing_photos.new

this should instance a new model of thing_photos. If you're not using nested routes you'll have to add the thing_id as a hidden field.

PJ Davis
A: 

The form should be for @thing_photo and should either pass in the thing_id or if it's a nested route you can get it from that

then in your thing_photos controller

@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 understand the question correctly, this might help.

Phil