Belongs_to and foreign_id troubles

Hi everyone,
I’m trying to connect an uploaded image (using act_as_attachment) to an
item in my database. Uploading the image in itself is no problem, though
I can’t seem to get the foreign-id to “stick”.

I’ve set up my models (image.rb and item.rb). In image.rb I’ve setup the
“belongs_to :item”-association and created an “item_id” in my
images-table using migrations (and I’ve confirmed it has been in fact
created in YourSQL).

In my uploads-controller I’ve setup the following:

def new
@item = Item.find(params[:id])
@image = Image.new
end

def create
@image = Image.create!(params[:image])
@image.item = params[:id]
redirect_to :action => ‘list’
end

(I’ve tried switching @image.item to @image.item_id, and params[:id] to
params[:item])

To upload a new image I enter the URL: “/uploads/new/12”. My
uploads-view looks like this:

Product: <%= @item.name %>

<% form_for :image, :url => { :action => ‘create’ }, :html => {
:multipart => true } do |f| -%>

<%= f.file_field :uploaded_data %>

<%= submit_tag :Create %>

<% end -%>

My log shows no command to input item_id to be found…

Processing UploadsController#create (for 127.0.0.1 at 2007-03-03
20:05:55) [POST]
Session ID: fd42ffbe649cc70a002b53acd94926e1
Parameters: {“commit”=>“Create”, “action”=>“create”,
“controller”=>“uploads”,
“image”=>{“uploaded_data”=>#<File:/tmp/CGI.830.1>}}
Image Columns (0.037518) SHOW FIELDS FROM images
SQL (0.002857) BEGIN
SQL (0.000795) INSERT INTO images (content_type, item_id,
size, thumbnail, db_file_id, filename, height, parent_id,
width) VALUES(‘image/jpeg’, NULL, 168427, NULL, NULL, ‘DSC00414.jpg’,
428, NULL, 640)
SQL (0.029149) COMMIT

Thanks for any help!

Best regards,
Gustav

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs