1
votes

I'm trying to upload multiple files on a form. I followed the rail casts for paperclip and nested attributes, and also this tutorial http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/ but I can't seem to get it working...

I've also searched here in stack overflow, looked at all paperclip and nested attributes posts, but I can't seem to find my answer, it seems that I'm doing everything right...

What happens is that when I submit the form, it creates the ad (it's an ad app), it says everything is ok, but it doesn't write the image data to the database and also it doesn't upload the files...

So I have the Classified model:

class Classified < ActiveRecord::Base
has_many :classified_images, :dependent => :destroy

accepts_nested_attributes_for :classified_images, :reject_if => lambda { |t| t['classified_image'].blank? }

attr_accessible :classified_images_attributes, :access, :contact, :price, :bizType 
end

Then, the Classified_Image model:

class ClassifiedImage < ActiveRecord::Base
belongs_to :classified
has_attached_file :photo, :styles => {:small => "150x150>", :large => "320x240>"},
  :url => "/assets/products/:id/:style/:basename.:extension",
  :path => ":rails_root/public/assets/classifieds/:id/:style/:basename.:extension"

validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes

attr_accessible :caption, :photo
end

On the Classified controller, on the "new" part, I have: def new @classified = Classified.new

3.times { @classified.classified_images.build }

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @classified }
end

end

On the "_form" I have:

<%= form_for @classified, :html => { :multipart => true } do |f| %>
...
<%= f.fields_for :classified_images do |builder| %>
<%= render 'image_fields', :f => builder %>
<% end %>

On the "image_fields" partial I have:

<% if f.object.new_record? %>
<li>
<%= f.label :caption %>
<%= f.text_field :caption %>
<%= f.label :photo %>
<%= f.file_field :photo %>
</li>
<% end %>

On the migration files I have:

class AddAttachmentPhotoToClassifiedImages < ActiveRecord::Migration
def self.up
  add_attachment :caption, :classified_id, :photo
end

def self.down
drop_attached_file :caption, :classified_id, :photo
end
end

class CreateClassifiedImages < ActiveRecord::Migration
def change
create_table :classified_images do |t|
  t.string :caption
  t.integer :classified_id

  t.timestamps
end
end
end

On the "development.rb" file I have:

 Paperclip.options[:command_path] = "/usr/local/bin/"
 Paperclip.options[:log] = true

Here's an example of the log when I commit the form:

Started POST "/classifieds" for 127.0.0.1 at 2013-05-19 23:39:43 +0100 Processing by ClassifiedsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"978KGJSUlmMEvr6Tysg5xYIEQzNLn5vod07g+Z7njkU=", "classified"=>{"contact"=>"918218338", "price"=>"1500", "access"=>"bons", "classified_images_attributes"=>{"0"=>{"caption"=>"teste", "photo"=>#@original_filename="064_dont-count-the-days.jpg", @content_type="image/jpeg", >@headers="Content-Disposition: form-data; name=\"classified[classified_images_attributes][0][photo]\"; filename=\"064_dont-count-the-days.jpg\"\r\nContent-Type: image/jpeg\r\n", >@tempfile=#3954-11t04t>>}, "1"=>{"caption"=>""}, "2"=>{"caption"=>""}}}, "commit"=>"Criar novo >Classificado"} (0.1ms) begin transaction SQL (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at", "price",) >VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at", Sun, 19 >May 2013 22:39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun, 19 May 2013 22:39:43 UTC >+00:00]] (0.8ms) commit transaction Redirected to localhost:3000/classifieds/8 Completed 302 Found in 5ms (ActiveRecord: 1.4ms)

As you can see, it inserts into the "classifieds" table but not into the "classifieds_image" table, and also, I don't get any info from paperclip...

Sorry for all the code but this should be really something simple that i'm not seeing and as more information you've got, the better you can help me... Please let me know if you need any more code or info...

1

1 Answers

1
votes

We spent days chasing a similar problem. In the end it was the :reject_if lambda for the accepts_nested_attributes_for call in the model that triggered in the wrong situations.

Now that I revisit the question, it seems that you have the same issue. Instead of:

:reject_if => lambda { |t| t['classified_image'].blank? }

you should probably have:

:reject_if => lambda { |t| t['photo'].blank? }

i.e. the name of the paperclip attribute instead of the nesting model.


It is a frustrating thing to get wrong since it fails silently, t['classified_image'] will be nil all the time and your attributes will be rejected as specified. :) At least we learned to be more careful with :reject_if...