ruby on rails - Paperclip, multiple attachments and validation -
does have rails 3 example of multiple attachments working validation on multipart form? i've been trying working forever (and have found every blog post , message could, none cover situation, , docs don't @ all).
the first problem examples use 'new_record?' in view template, returns true in new/create sequence when validation fails because no model instances have been saved (so no 'id' value). if start 5 model instances/file inputs , upload 1 file, have 6 file inputs presented when re-render new view, , 'unless' clause fails same reason , no thumbnails presented.
i want preserve link uploaded file (and know possible--they're living in temp directory) while presenting validation errors user other required fields.
somebody somewhere must have working paperclip. ;)
the way i'm using:
i have properties has many photos (10 in case). going code whe have:
in properties controller:
def new @search = property.search(params[:search]) @property = property.new 10.times { @property.photos.build } respond_to |format| format.html # new.html.erb format.xml { render :xml => @property } end end # /properties/1/edit def edit @search = property.search(params[:search]) @property = property.find(params[:id]) # se o usuário atual dono da propriedade if current_user.id == @property.user_id @photos = photo.where("property_id = ?", @property.id) @n = @photos.count @n = 10-@n @n.times { @property.photos.build } else render :action => "show" end end
10.times "render" 10 times photo field in view. when in edit form, apper photo fields lefting. exemple: @ 1st time uploaded 3 photos, if want upload more, 7 fields appear.
in property model have:
class property < activerecord::base attr_accessible :photos_attributes, :logradouro, :complemento, :wc, :negocio, :cep, :vagas, :valor, :quartos, :uf, :area, :bairro, :suites, :salas, :numero, :cidade, :descricao, :status, :tipoimovel has_many :photos accepts_nested_attributes_for :photos, :allow_destroy => true end
this allow photos uploaded.
photo model:
class photo < activerecord::base belongs_to :property has_attached_file :photo, :styles => { :small => "100x100>", :medium => "530>x530", :large => "800x800>" } validates_attachment_presence :photo validates_attachment_size :photo, :less_than => 500.kilobytes end
in form partial:
<div id="new_up"> <%= f.fields_for :photos |p| %> <% if p.object.new_record? %> <p><%= p.file_field :photo %> <%= p.radio_button :miniatura, true -%> </p> <% end %> <% end %> </div> <div id="old_up"> <h4>imagens atuais</h4> <% f.fields_for :photos |p| %> <% unless p.object.new_record? %> <div style="float: left;"> <%= p.radio_button :miniatura, true -%> <%= link_to image_tag(p.object.photo.url(:small)), p.object.photo.url(:original) %> <%= p.check_box :_destroy %> </div> <% end %> <% end %> </div>
Comments
Post a Comment