Hi,
I have to models, 'label' and 'image':
label has_one :image
image belongs_to :label
I am trying to generate a single form using the info in this tutorial. So that a user can add a new label and upload an image at the same time. I have also installed the attachment_fu and mini-magick plugins...
Below are my files:
The images migration/table;
class CreateImages < ActiveRecord::Migration
def self.up
create_table :images do |t|
t.column :parent_id, :integer
t.column :content_type, :string
t.column :filename, :string
t.column :thumbnail, :string
t.column :size, :integer
t.column :width, :integer
t.column :height, :integer
t.column :label_id, :integer
end
end def self.down
drop_table :images
end
end
new.rhtml;
<h2>Add new Label</h2><%= error_messages_for :label, :image %>
<% form_for(:label, @label, :url => { :action => 'create'}, :html => { :multipart => true }) do |f| %>
<p>
<label for="label_name">Name:</label><br/ >
<%= f.text_field :name %>
</p>
<p>
<label for="label_country">Country:</label><br/ >
<%= f.text_field :country %>
</p>
<p>
<label for="label_website">Website:</label><br/ >
<%= f.text_field :website %>
</p>
<p>
<label for="label_email">Email:</label><br/ >
<%= f.text_field :email %>
</p>
<% fields_for(:image, @label.image) do |i| %>
<p>
<label for="label_image">Image:</label><br />
<%= i.file_field :uploaded_data %>
</p>
<% end %>
<%= submit_tag 'Create' %>
<% end %>
label_controller.rb;
def new
@label = Label.new
@image = Image.new
enddef create
@label = Label.new(params[:label])
@image = @label.build_image(params[:image])
if @label.save
redirect_to :action => 'list'
else
render :action => 'new'
end
end
image.rb;
class Image < ActiveRecord::Base
belongs_to :labelhas_attachment :content_type => :image,
:storage => :file_system,
:path_prefix => 'public/upload',
:max_size => 500.kilobytes,
:resize_to => '320x200>',
:thumbnails => { :thumb => '100x100>' }
validates_as_attachment
end
When i submit the form, the new label is added, but there us no image uploaded, and the image tables is empty...
Any help appreciated...
Thanks,
Pj,
Last edited by pjay79 (2007-11-11 23:24:05)