回形针、多个附件和验证 [英] Paperclip, multiple attachments and validation

查看:58
本文介绍了回形针、多个附件和验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

某个地方的某个人必须使用 Paperclip 进行此操作.;)

解决方案

我使用的方式:

我的房产有很多照片(以防万一).转到代码:

在属性控制器中:

 def new@search = Property.search(params[:search])@property = Property.new10.times { @property.photos.build }response_to do |格式|format.html # new.html.erbformat.xml { 渲染:xml =>@财产 }结尾结尾# 获取/properties/1/edit定义编辑@search = Property.search(params[:search])@property = Property.find(params[:id])# Se o usuário atual for dono da propriedade如果 current_user.id == @property.user_id@photos = Photo.where("property_id = ?", @property.id)@N = @photos.count@N = 10-@N@N.times { @property.photos.build }别的渲染:动作=>表演"结尾结尾

10 倍渲染"10 倍视野中的照片.在编辑表单中,只剩下一些照片字段.举个例子:第一次我上传了 3 张照片,然后如果我想上传更多,只会出现 7 个字段.

<小时>

在属性模型中我有:

class 属性 真的结尾

这允许上传照片.

<小时>

照片模型:

班级照片{:小=>"100x100>", :medium =>"530>x530", :large =>800x800"}validates_attachment_presence :照片validates_attachment_size :photo, :less_than =>500.KB结尾

<小时>

以我的形式部分:

<%= f.fields_for :photos do |p|%><% 如果 p.object.new_record?%><p><%= p.file_field :photo %><%= p.radio_button :miniatura, true -%></p><%结束%><%结束%>

<div id="old_up"><h4>Imagens Atuais</h4><% f.fields_for :photos do |p|%><% 除非 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 %>

<%结束%><%结束%>

Does anybody have a Rails 3 example of multiple attachments working with validation on a multipart form? I've been trying to get this working forever (and have found every blog post and message I could, but none cover this situation, and the docs don't help at all).

The first problem is that most examples use 'new_record?' in the view template, but this always returns true in a new/create sequence when validation fails because no model instances have been saved (so no 'id' value). So if you start with 5 model instances/file inputs and upload one file, you now have 6 file inputs presented when you re-render the new view, and the 'unless' clause fails for the same reason and no thumbnails are presented.

I want to preserve the link to the uploaded file (and I know this is possible--they're living in a temp directory) while presenting validation errors to the user for other required fields.

Somebody somewhere must have this working with Paperclip. ;)

解决方案

The way I'm using:

I have properties that has many photos (10 in case). Going to the code whe have:

In properties controller:

 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 "render" 10 times the photo field in view. When in edit form, just apper photo fields lefting. For exemple: At 1st time I uploaded 3 photos, then if I want to upload more, just 7 fields appear.


In property model I 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 to be 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 my form partial:

<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>

这篇关于回形针、多个附件和验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆