# Attachment\_fu with extra db fields?

**URL:** <https://www.ruby-forum.com/t/attachment-fu-with-extra-db-fields/103940>\
**Category:** Rails\
**Created:** [May 11, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/attachment-fu-with-extra-db-fields/103940 "2007-05-11T20:08:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![supaspoida](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@supaspoida](https://www.ruby-forum.com/u/supaspoida)\
**Post date:** [May 11, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/attachment-fu-with-extra-db-fields/103940/1 "2007-05-11T20:08:10Z")

</div>

I have been working on this problem for two days and can’t seem to make  
an progress on it. I am positive that I am just missing something  
obvious. I am using attachment\_fu, I have created an image model and  
images controller, and everything is working fine. Now I want to have  
the uploaded images associated with a project using a project\_id column.  
I want to do this from the show page for each project, grabbing the  
project id from the params using a hidden field to pass to the model.  
Here is what I put into the show.rhtml view, presented by the projects  
controller:

\<%= error\_messages\_for :image %\>  
\<%= flash[:notice] %\>

\<% form\_for(:image, :url =\> { :controller =\> ‘images’, :action =\>  
‘create’ }, :html =\> { :multipart =\> true }) do |f| -%\>

Upload An Image: \<%= f.file\_field :uploaded\_data %\> \<%= hidden\_field 'project\_id', params[:id] %\>

\<%= submit\_tag 'Create' %\>

\<% end -%\>

When I submit that it does all the image stuff fine, but does not pass  
the project id. When I look at the generated source the project id is  
showing up in the form like this:

Upload An Image:

Am I losing the id in the redirect to the images controller? Can anybody  
shed some light on the proper way to do this? I haven’t had any luck  
searching around the web. Help is much appreciated!

---

<div class="post-metadata">

**Author:** ![supaspoida](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@supaspoida](https://www.ruby-forum.com/u/supaspoida)\
**Post date:** [May 11, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/attachment-fu-with-extra-db-fields/103940/2 "2007-05-11T23:11:30Z")

</div>

Might want to try hidden\_field\_tag vs. hidden\_field seeing you are not  
using the f on hidden\_field as part of your form.

\<%=hidden\_field\_tag :field\_name, field\_value%\>

Scott

On May 11, 11:08 am, Lar Van der jagt \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![supaspoida](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/51bf81/32.png) [@supaspoida](https://www.ruby-forum.com/u/supaspoida)\
**Post date:** [May 12, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/attachment-fu-with-extra-db-fields/103940/3 "2007-05-12T23:58:30Z")

</div>

Howdy,

The reason is that the name of the form field for the project id is  
wrong.

See how the file upload is image[uploaded\_data] ? That is being  
parsed in the controller into the image object but the project\_id is  
not because of how it is setup.

Try this for the hidden field:

\<%= hidden\_field\_tag ‘image[project\_id]’, params[:id] %\>

I haven’t tried this but you see what I’m getting at…

Or, keep your original and then in the controller, something like:

image.project = Project.find(params[:project\_id])

Hunter
