【问题标题】:Paperclip, multiple attachments and validation回形针、多个附件和验证
【发布时间】:2011-01-26 02:41:39
【问题描述】:

有没有人有 Rails 3 示例,其中多个附件在多部分表单上进行验证?我一直在努力让它永远工作(并且找到了我能找到的每一篇博客文章和消息,但没有一个涵盖这种情况,而且文档根本​​没有帮助)。

第一个问题是大多数示例都使用“new_record”?在视图模板中,但是当验证失败时,这总是在新/创建序列中返回 true,因为没有保存模型实例(所以没有 'id' 值)。因此,如果您从 5 个模型实例/文件输入开始并上传一个文件,那么当您重新渲染新视图时,您现在会显示 6 个文件输入,并且“除非”子句由于同样的原因而失败并且不显示缩略图。

我想保留上传文件的链接(我知道这是可能的——它们位于临时目录中),同时向用户显示其他必填字段的验证错误。

必须有人在某个地方使用 Paperclip。 ;)

【问题讨论】:

  • 我也在找同样的东西,你有没有发现什么?谢谢。
  • Paperclip 没有任何东西,至少不是完整的解决方案(在emersonlackey.com/article/rails-paperclip-multiple-file-uploads 可以看到多个附件支持没有验证)。不久前,我为 Sinatra 应用程序编写了类似的代码,但花了很长时间才把它弄好。目前我只是不为用户提供这种支持(如果我表单中其他字段的验证失败,他们将不得不重新上传)。我认为这是使用“80%”解决方案时的挑战——从头开始编码比扩展更容易。

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


【解决方案1】:

我的使用方式:

我的房产有很多照片(如果有 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.times “渲染” 10 倍视野中的照片。 在编辑表单中,只需留下照片字段。 举个例子: 第一次我上传了 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>

【讨论】:

    【解决方案2】:

    我选择的答案是使用载波而不是回形针。我只用了大约一个小时就完成了切换,它开箱即用地解决了这个问题。

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

    【讨论】:

      【解决方案3】:

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

      第一个模型是您真正想要的模型。比方说传记。

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多