I'm trying to do a file upload. I have followed the examples in Agile
Web Development with Rails, and Rails Cookbook, and I can't get it to
work!! The following code is from the Rails Cookbook. I have two
tables: Items & Photos. It's supposed to save Name and Description
into the Items table, and then grab the photo information and put it
in the Photo table. The Item information is saving, but I'm blowing
up in the Photo Model. It doesn't recognize the field names for the
Photo table and also doesn't recognize the "read" attribute for the
image. Any idea what I'm missing?? As always, any and all help is
greatly appreciated!!! Thanks! ~Ali
@item = Item.new(params[:item])
# I don't understand this part -- how can it ever get past here???
I tried commenting it out, but still won't work.
flash[:error] = 'Problem Uploading'
redirect_to :action => 'new'
@photo = Photo.new(params[:photo])
@photo.item_id = @item.id
flash[:notice] = 'Item was successfully created.'
redirect_to :action => 'list'
flash[:error] = 'There was a problem.'
render :action => 'new'
class Item < ActiveRecord::Base
class Photo < ActiveRecord::Base
belongs_to :item, :foreign_key => "item_id"
self.name = base_part_of(image_field.original_filename)
self.content_type = image_field.content_type.chomp
self.data = image_field.read
name = File.basename(file_name)
<div class="errorExplanation p"><%= flash[:error] %></div>
<%= text_field 'item', 'name' %></p>
<%= text_field 'item', 'description' %></p>
<%= file_field('photo', 'photo') %></p>
<%= start_form_tag :action => 'create', :id => @item, :multipart =>
<%= render :partial => 'form' %>
<%= submit_tag "Create" %>
<%= end_form_tag %>