2011-01-26 78 views
6

有沒有人有多個附件的工作與多部分窗體驗證的Rails 3示例?我一直試圖讓這個工作永遠(並發現每一個博客文章和消息,我可以,但沒有涵蓋這種情況,和文檔根本沒有幫助)。回形針,多個附件和驗證

第一個問題是大多數例子使用'new_record?'在視圖模板中,但是當驗證失敗時,這通常在新/創建序列中返回true,因爲沒有保存模型實例(所以沒有'id'值)。因此,如果您從5個模型實例/文件輸入開始並上傳一個文件,那麼當您重新呈現新視圖時,現在會顯示6個文件輸入,並且由於同樣的原因'unless'子句失敗並且沒有顯示縮略圖。

我想保留鏈接到上傳的文件(我知道這是可能的 - 他們生活在一個臨時目錄中),同時向用戶提交其他必填字段的驗證錯誤。

某個地方的人必須使用Paperclip。 ;)

+0

我在尋找同樣的事情,你有沒有發現什麼呢?謝謝。 – 2011-02-04 22:10:27

回答

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 
4

的辦法,我使用:

我有屬性,有許多照片(10時)。要WHE具有的代碼:

在屬性控制器:鑑於

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 = [email protected]  
     @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>