Forum: Ruby on Rails attachment_fu with extra db fields?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
F63093af8fa0beb7329d848da490d6aa?d=identicon&s=25 Lar Van der jagt (supaspoida)
on 2007-05-11 20:08
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

<%= error_messages_for :image %>
<%= flash[:notice] %>

<% form_for(:image, :url => { :controller => 'images', :action =>
'create' }, :html => { :multipart => true }) do |f| -%>
    <label for="image">Upload An Image:</label>
    <%= 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:

<form action="/images/create" enctype="multipart/form-data"
method="post">  <p>
    <label for="image">Upload An Image:</label>
    <input id="image_uploaded_data" name="image[uploaded_data]"
size="30" type="file" />
    <input id="project_id_1" name="project_id[1]" type="hidden" />
    <input name="commit" type="submit" value="Create" />


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!
125639ddcc0b9119bb84e29416ace63a?d=identicon&s=25 Scott (Guest)
on 2007-05-11 23:11
(Received via mailing list)
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%>


On May 11, 11:08 am, Lar Van der jagt <rails-mailing-l...@andreas-
A7db9ec803b5895ae5f916a74e2db329?d=identicon&s=25 Hunter Hillegas (Guest)
on 2007-05-12 23:58
(Received via mailing list)

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

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])

This topic is locked and can not be replied to.