# Attachment\_fu inside a form for a different model

**URL:** <https://www.ruby-forum.com/t/attachment-fu-inside-a-form-for-a-different-model/127362>\
**Category:** Rails\
**Created:** [January 28, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/attachment-fu-inside-a-form-for-a-different-model/127362 "2008-01-28T17:03:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 28, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/attachment-fu-inside-a-form-for-a-different-model/127362/1 "2008-01-28T17:03:33Z")

</div>

I’m getting very confused trying to use attachment\_fu, but i’m sure the  
answer is simple to one of you…

I have a model called ‘MusicService’, which has a logo associated with  
it. Logos have been set up to belong\_to :music\_service, and  
music\_service has\_one :logo. Logo is also set up to be an attachment,  
with has\_attachment and the usual parameters. So far so good.

On the edit page for music\_service is where i’m having problems - along  
with all the other fields for music\_service i have a file\_field for the  
logo file. Whereas the other fields write into a params field called  
‘name’, ‘address’ etc, the file\_field is writing into :uploaded\_data.  
When i submit the form, the ‘update’ controller action is called. How  
do i deal with :uploaded\_data in the controller?

I’m using attachment\_fu elsewhere, where i have a form to edit a single  
attachment object, and that works fine, it’s just incorporating it into  
a larger form for a different model that’s got me snarled up.

Any suggestions?  
thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 29, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/attachment-fu-inside-a-form-for-a-different-model/127362/2 "2008-01-29T13:48:15Z")

</div>

On 1/28/08, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> with all the other fields for music\_service i have a file\_field for the  
> thanks  
> Do you have the file\_field inside a fields\_for block in your view?

something like.

\<% form\_for @music\_service, :html =\> {:multipart =\> true}) do |f| %\>  
…  
\<% f.fields\_for :logo, @music\_service.logo do |logo| %\>  
\<%= logo.file\_field “uploaded\_data” %\>  
\<% end %\>  
…  
\<% end %\>

Note the :html =\> {:multipart =\> true} on the outer form\_for, it’s  
important.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 29, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/attachment-fu-inside-a-form-for-a-different-model/127362/3 "2008-01-29T14:38:29Z")

</div>

Rick Denatale wrote:

> \<% form\_for @music\_service, :html =\> {:multipart =\> true}) do |f| %\>  
> …  
> \<% f.fields\_for :logo, @music\_service.logo do |logo| %\>  
> \<%= logo.file\_field “uploaded\_data” %\>  
> \<% end %\>  
> …  
> \<% end %\>

Ah…so that’s how you do nested forms! Nice one, that was what i was  
stuck on i think.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [May 2, 2009, 2:26am UTC](https://www.ruby-forum.com/t/attachment-fu-inside-a-form-for-a-different-model/127362/4 "2009-05-02T02:26:34Z")

</div>

Thanks a lot… I was searching for the exact same thing…

But if you are using this form for creating a New file upload, you have  
to instantiate the nested object too.

example -  
for the code given above

> \<% form\_for @music\_service, :html =\> {:multipart =\> true}) do |f| %\>  
> …  
> \<% f.fields\_for :logo, @music\_service.logo do |logo| %\>  
> \<%= logo.file\_field “uploaded\_data” %\>  
> \<% end %\>  
> …  
> \<% end %\>

In the controllers new method/action you should put in a line

@music\_service = MusicService.new # there by default  
@music\_service.logo = Logo.new # Add this to avoid an error related  
to new\_record?
