attachment_fu inside a form for a different model

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

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 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.

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?