# Attachment\_fu. . .HTML involved in immediately associating a

**URL:** <https://www.ruby-forum.com/t/attachment-fu-html-involved-in-immediately-associating-a/103446>\
**Category:** Rails\
**Created:** [May 6, 2007, 2:51am UTC](https://www.ruby-forum.com/t/attachment-fu-html-involved-in-immediately-associating-a/103446 "2007-05-06T02:51:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [May 6, 2007, 2:51am UTC](https://www.ruby-forum.com/t/attachment-fu-html-involved-in-immediately-associating-a/103446/1 "2007-05-06T02:51:49Z")

</div>

All,

I’m having trouble wrapping my head around how to associate my IMAGE  
model with my PRODUCT model. Basically I have the obligatory fields  
for the attachment\_fu association:

class CreateImages \< ActiveRecord::Migration

def self.up  
create\_table :images do |t|  
t.column :parent\_id, :integer  
t.column :content\_type, :string  
t.column :filename, :string  
t.column :thumbnail, :string  
t.column :size, :integer  
t.column :width, :integer  
t.column :height, :integer  
end  
end

def self.down  
drop\_table :images  
end  
end

. . .then I have the models associated through “has\_many” and  
“belongs\_to”. What I can’t quite figure out is how to immediately  
associate an uploaded image with a product on the same “new” page.  
Would I set the “form\_for” a PRODUCT and IMAGE independently, or would  
I have one nested inside the other? I’m sorry, I’ve been coding for  
several hours on end now, and I’m admittedly a bit rusty on my HTML  
form handling.

Thanks,  
Michael

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [May 6, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/attachment-fu-html-involved-in-immediately-associating-a/103446/2 "2007-05-06T12:47:28Z")

</div>

. . .anyone?

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [May 6, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/attachment-fu-html-involved-in-immediately-associating-a/103446/3 "2007-05-06T19:55:01Z")

</div>

Basically I need to include two models in a single form. I guess what  
I’m looking for is very similar to this thread:

[http://www.railsforum.com/viewtopic.php?id=717](http://www.railsforum.com/viewtopic.php?id=717)

. . .but I’m not sure how to include the specific information for the  
attachment\_fu based IMAGE model (e.g. url, html {multipart. . . . .})

Thanks,  
Michael

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [May 8, 2007, 12:35pm UTC](https://www.ruby-forum.com/t/attachment-fu-html-involved-in-immediately-associating-a/103446/4 "2007-05-08T12:35:00Z")

</div>

> 

Michael,  
approach that I am using is to nest ‘documents’ (or images) in product  
form so field for attachment\_fu upload looks something like these:

then, in model specify attributes which are set from controller

attr\_writer :edit\_documents, :new\_documents  
after\_save :update\_documents

and code for handling actual uploads looks like:

def update\_documents  
opts = @edit\_documents || {}  
documents.each { |opt| opt.destroy if !opts.has\_key?(opt.id.to\_s) }  
opts.each {|key, params|  
Document.find(key).update\_attributes(params)  
}  
(@new\_documents || []).each { |p|  
next if (p[:uploaded\_data].size == 0 && p[:position].empty? &&  
p[:name].empty?)  
self.documents.create(p)  
}  
end

hope that helps a little,  
Bojan

–  
Bojan M.  
 → Ruby on Rails and Web D. Blog : [http://source.mihelac.org](http://source.mihelac.org)

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [May 7, 2007, 8:47am UTC](https://www.ruby-forum.com/t/attachment-fu-html-involved-in-immediately-associating-a/103446/5 "2007-05-07T08:47:52Z")

</div>

Interesting I am working with this same problem all day today…

These posts were most helpful to me, though w/ my barely functioning  
app I still have lots of questions myself!

> **[Using attachment\_fu to add image attachment support to your Rails application](https://significantbits.wordpress.com/2007/04/06/using-attachment_fu-by-techno-weenie-to-add-image-attachment-support-to-your-rails-application/)**
>
> Ever wondered how to easily add image attachment support to your Rails application? Then you should definitely give attachment\_fu a go, a very easy to use Rails plugin by Rick Olson. (Note: This ar…

[http://railsgrunt.com/2007/3/14/ruby-rails-upload-multiple-files-using-acts-as-attachment](http://railsgrunt.com/2007/3/14/ruby-rails-upload-multiple-files-using-acts-as-attachment)

My end goal is to hopefully get to having an object with multiple  
images attachable and editable through a single, dynamic form. If you  
find any more articles or code examples on how to do this, please  
post!

Hope these help. Good luck!

Hal

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [May 9, 2007, 4:21am UTC](https://www.ruby-forum.com/t/attachment-fu-html-involved-in-immediately-associating-a/103446/6 "2007-05-09T04:21:26Z")

</div>

Bojan,

Thanks for the post. I’m still slightly confused. The code you  
posted for the form looks like straight HTML not RHTML. Could you  
clarify a bit as to what is goin on there and perhaps ‘rubify’ it?

Thanks,  
Michael

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [May 10, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/attachment-fu-html-involved-in-immediately-associating-a/103446/7 "2007-05-10T21:54:26Z")

</div>

Perhaps I should handle it in the save of the Product? If  
the :uploaded\_data var is not nil, then use it. Any thoughts?

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [May 11, 2007, 2:41am UTC](https://www.ruby-forum.com/t/attachment-fu-html-involved-in-immediately-associating-a/103446/8 "2007-05-11T02:41:53Z")

</div>

Michael, I take the code from HTML page as it is created with ROAR and  
it would be too much to post all the code here.  
I recommend you to download Roar and see how it is done there / it has  
many examples and maybe u’d like interface too.

best,  
Bojan

gberz3 wrote:

> > > . . .then I have the models associated through “has\_many” and  
> > > form so field forattachment\_fuupload looks something like these:
> > 
> > ```
> > }
> > 
> > ```

–  
Bojan M.  
 → Ruby on Rails and Web D. Blog : [http://source.mihelac.org](http://source.mihelac.org)

---

<div class="post-metadata">

**Author:** ![gberz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f9ae1b/32.png) [@gberz3](https://www.ruby-forum.com/u/gberz3)\
**Post date:** [May 11, 2007, 2:42am UTC](https://www.ruby-forum.com/t/attachment-fu-html-involved-in-immediately-associating-a/103446/9 "2007-05-11T02:42:53Z")

</div>

yes, you can handle it in Product.

gberz3 wrote:

> > > form handling.
> > 
> > ```
> > }
> > 
> > ```
> > 
> > –  
> > Bojan M.  
> > → Ruby on Rails and Web D. Blog :[http://source.mihelac.org](http://source.mihelac.org)
> 
> >

–  
Bojan M.  
 → Ruby on Rails and Web D. Blog : [http://source.mihelac.org](http://source.mihelac.org)
