sqlite - Paperclip and nested attributes - not writing to the database -
i'm trying upload multiple files on form. followed rail casts paperclip , nested attributes, , tutorial http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/ can't seem working...
i've searched here in stack overflow, looked @ paperclip , nested attributes posts, can't seem find answer, seems i'm doing right...
what happens when submit form, creates ad (it's ad app), says ok, doesn't write image data database , doesn't upload files...
so have 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, 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 classified controller, on "new" part, have: def new @classified = classified.new
3.times { @classified.classified_images.build } respond_to |format| format.html # new.html.erb format.json { render json: @classified } end end
on "_form" have:
<%= form_for @classified, :html => { :multipart => true } |f| %> ... <%= f.fields_for :classified_images |builder| %> <%= render 'image_fields', :f => builder %> <% end %>
on "image_fields" partial have:
<% if f.object.new_record? %> <li> <%= f.label :caption %> <%= f.text_field :caption %> <%= f.label :photo %> <%= f.file_field :photo %> </li> <% end %>
on migration files 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 |t| t.string :caption t.integer :classified_id t.timestamps end end end
on "development.rb" file have:
paperclip.options[:command_path] = "/usr/local/bin/" paperclip.options[:log] = true
here's example of log when commit form:
started post "/classifieds" 127.0.0.1 @ 2013-05-19 23:39:43 +0100 processing classifiedscontroller#create 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 "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 localhost:3000/classifieds/8 completed 302 found in 5ms (activerecord: 1.4ms)
as can see, inserts "classifieds" table not "classifieds_image" table, , also, don't info paperclip...
sorry code should simple i'm not seeing , more information you've got, better can me... please let me know if need more code or info...
we spent days chasing similar problem. in end :reject_if
lambda accepts_nested_attributes_for
call in model triggered in wrong situations.
now revisit question, seems have same issue. instead of:
:reject_if => lambda { |t| t['classified_image'].blank? }
you should have:
:reject_if => lambda { |t| t['photo'].blank? }
i.e. name of paperclip attribute instead of nesting model.
it frustrating thing wrong since fails silently, t['classified_image']
nil
time , attributes rejected specified. :) @ least learned more careful :reject_if
...
Comments
Post a Comment