So, I have this in my model:
# app/models/interior.rb
class Interior < ActiveRecord::Base
belongs_to :casa
has_attached_file :plano,
:path => ":rails_root/public/planos_unidades/:styles/:id.:content_type_extension",
:url => "/planos_unidades/:styles/:id.:content_type_extension",
:styles => { :thumb => '98x98' },
:convert_options => {
:thumb => '-background "#f8f8f8" -gravity center -extent 98x98'
}
end
# app/models/casa.rb
class Casa < ActiveRecord::Base
has_many :interiors, :dependent => :destroy
accepts_nested_attributes_for :interiors
end
And this in my create/edit form:
- form_for(:casa, :url => { :action => :save, :id => @casa.id }, :html => { :id => "casa_form", :multipart => true }) do |casa|
- @casa.interiors.each_with_index do |interior, index|
- fields_for "casa[interiors_attributes][#{index}]", interior do |interior|
= interior.file_field(:plano)
Where index
is a counter, in order to have multiple interiors
in the same casa
. I mean, when the user clicks "Add Interior" I render via AJAX the next:
- fields_for "casa[interiors_attributes][#{index}]", interior do |interior|
= interior.file_field(:plano)
with an increasing value for index, of course.
Anyway, this works great for creating a casa
and its respectives interiors
, but when I want to edit it, I'm unable to retrieve the content for the :plano
field, so when the user hit "Save", Paperclip decides it would be the hell of funny to delete the previously uploaded and saved :plano
file! "Sorry" - it saids - ", but the :plano
field was empty" @_@