回形针,多个附件和validation

有没有人有多个附件的Rails 3示例在多部分表单上进行validation? 我一直在努力让这个工作永远发挥作用(我发现每篇博文和消息都可以,但没有一个能够涵盖这种情况,而且文档根本​​没有帮助)。

第一个问题是大多数例子都使用’new_record?’ 在视图模板中,但是当validation失败时,它总是在new / create序列中返回true,因为没有保存模型实例(所以没有’id’值)。 因此,如果您从5个模型实例/文件输入开始并上传一个文件,则在重新呈现新视图时,您现在有6个文件输入,并且’unless’子句因同样的原因而失败,并且没有显示缩略图。

我想保留上传文件的链接(我知道这是可能的 – 他们生活在临时目录中),同时向用户提供其他必填字段的validation错误。

有人在某处必须使用Paperclip。 ;)

我正在使用的方式:

我有很多照片的属性(10个案例)。 去代码有:

在属性控制器中:

def new @search = Property.search(params[:search]) @property = Property.new 10.times { @property.photos.build } respond_to do |format| format.html # new.html.erb format.xml { render :xml => @property } end end # GET /properties/1/edit def edit @search = Property.search(params[:search]) @property = Property.find(params[:id]) # Se o usuário atual for 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.在视野中“渲染”10倍的照片视野。 在编辑forms时,只需要apper photo fields lefting。 例如:我第一次上传了3张照片,如果我想上传更多,只会显示7个字段。


我在物业模型中有:

 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 

这样可以上传照片。


照片型号:

 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 

在我的forms部分:

 
<%= f.fields_for :photos do |p| %> <% if p.object.new_record? %>

<%= p.file_field :photo %> <%= p.radio_button :miniatura, true -%>

<% end %> <% end %>

Imagens Atuais

<% f.fields_for :photos do |p| %> <% unless p.object.new_record? %>
<%= p.radio_button :miniatura, true -%> <%= link_to image_tag(p.object.photo.url(:small)), p.object.photo.url(:original) %> <%= p.check_box :_destroy %>
<% end %> <% end %>

我接下来的答案是使用carrierwave而不是paperclip。 我花了大约一个小时才完成切割,它解决了这个问题。

这是我在另一个更详细的线程中的答案: 当由于validation错误而无法保存模型时,不会丢失回形针附件

你可以通过使用三个模型和一点控制器魔法来实现这一点。

第一个模型是您真正想要的模型。 让我们说传记。

 class Biography < ActiveRecord::Base has_one :biography_fields has_many :biography_attachments end class BiographyFields < ActiveRecord::Base belongs_to :biography # Validations end class BiographyAttachment < ActiveRecord::Base belongs_to :biography # Paperclip stuff end 

现在在您的控制器中,您可以执行以下操作:

 class BiographiesController def some_method @biography = Biography.find(params[:biography_id]) || Biography.create! if @biography.biography_data.create(params[:biography_data]) # Try to save the uploads, if it all works, redirect. else # Still try the uploads, if they work, save them and make sure to pass the :biography_id as a hidden field so they can be used without re-uploading. Also reflect this in the form. end end end