回形针,多个附件和验证

时间:2011-01-26 02:41:40

标签: ruby-on-rails ruby-on-rails-3 paperclip

是否有人在多部分表单上使用Rails 3多个附件的示例?我一直在努力让这个工作永远发挥作用(并且我已经找到了每篇博文和消息,但没有一篇报道这种情况,而且文档根本​​没有帮助。)

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

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

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

3 个答案:

答案 0 :(得分:4)

我正在使用的方式:

我有很多照片的属性(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倍的照片。 在编辑形式时,只需要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

以我的形式部分:

<div id="new_up">            
            <%= f.fields_for :photos do |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 do |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>

答案 1 :(得分:2)

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

这是我在另一个更详细的帖子中的答案:Not losing paperclip attachment when model cannot be saved due to validation error

答案 2 :(得分:1)

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

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

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
相关问题