2010-10-22 70 views
0

我有兩個模型,Product和ProductImage,我想提供給每個產品添加多達6個圖像的可能性。form.fields_for做有限次

產品

has_many :product_images, :dependent => :destroy 

而ProductImage

belongs_to :product 

到目前爲止,我的看法如下:

#products/_form.html.erb. 
    <% @product.product_images.build %> 
    <%= form_for(@product, :html => { :multipart => true }) do |product_form| %> 
     <% if @product.errors.any? %> 
     <div id="error_explanation"> 
      <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2> 

      <ul> 
      <% @product.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
      <% end %> 
      </ul> 
     </div> 
     <% end %> 

     <div class="field"> 
     <%= product_form.label :title %><br /> 
     <%= product_form.text_field :title %> 
     </div> 
     <div class="field"> 
     <%= product_form.label :description %><br /> 
     <%= product_form.text_area :description %> 
     </div> 
     <div class="field"> 
     <%= product_form.label :price %><br /> 
     <%= product_form.text_field :price %> 
     </div> 
     <div id="product_images"> 
      <%= render :partial => 'product_images/form', :locals => {:form => product_form} %> 
     </div> 
     <div class="actions"> 
     <%= product_form.submit %> 
     </div> 
    <% end %> 

#product_images/_form.html.erb 
    <%= form.fields_for :product_images do |product_image_form| %> 
     <div class="image"> 
      <%= product_image_form.label :image, 'Image:' %> 
      <%= product_image_form.file_field :image %> 
     </div> 
     <% unless product_image_form.object.nil? || product_image_form.object.new_record? %> 
     <div class="image"> 
      <%= product_image_form.label :_destroy, 'Remove:' %> 
      <%= product_image_form.check_box :_destroy %> 
     </div> 
     <% end %> 
    <% end %> 

的問題是:我怎麼逼我的局部形式打印出6個不同的file_fields,每一個都有自己獨特的屬性名稱,例如:

name="product[product_images_attributes][0][image]" 
name="product[product_images_attributes][1][image]" 

等等?

我的意思是,甚至可能或有更好的和不同的方式來實現這樣的結果?

我在想我可以使用link_to和AJAX添加儘可能多的字段,但我想用戶可以輕鬆打印出六個字段並準備好,而不是每次點擊鏈接以添加字段。

回答

2

假設你在你的產品型號使用accepts_nested_attributes_for :product_images,你需要的東西就像你ProdcutsController如下:

def new 
    @product = Product.new 
    6.times { @product.product_images.build } # this will create 6 "blank product_images" 
end 

def edit 
    @product = Product.find(params[:id]).includes(:product_images) 
    (6 - @product.product_images.size).times { @product.product_images.build } # this will create as many "blanks" as needed 
end 

對於新產品,你build 6個新產品圖片,對現有產品你build只之多你需要爲了有6.

編輯:也刪除<% @product.product_images.build %>線從您的產品/ _form.html.erb視圖,因爲你是build相關的實例,而不是你的控制器。

+0

完美的作品。非常感謝,Teoulas。 – Neko 2010-10-22 17:05:23

+0

Teoulas,你知道這類事情在哪裏被記錄嗎?我真的想詳細瞭解如何處理嵌套屬性,但文檔在這個主題上真的很少。 – vinhboy 2010-10-25 04:30:13

+0

如果您想了解更多關於嵌套屬性的內部信息,您可以隨時查看代碼。這個概念非常簡單,它爲相關的模型屬性設置了一些getter/setter方法。以下是官方文檔: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html – Teoulas 2010-10-25 09:57:42