我有一個頁面,我可以查看產品,並直接從此頁面用戶可以添加和刪除與產品相關聯的多個圖像。我在表單中使用回形針上傳新文件。Rails與模型協會回形針
由於可以爲產品保存多個圖像,因此我創建了屬於產品模型的圖像模型。由於關聯,無法使用默認的回形針文件輸入。我的解決方案正在工作,但我想知道是否有一個更好的方式來實現這個沒有我砍的所有html的工作。
class Image < ActiveRecord::Base
belongs_to :product
class Product < ActiveRecord::Base
has_many :images, :dependent => :destroy accepts_nested_attributes_for :images, :allow_destroy => true
show.html.erb
<% @product.images.build %>
<%= form_for(@product, :html => { :multipart => true }) do |f| %>
<input id="image" name="product[images_attributes][<%= @product.images.count %>][photo]" >size="30" type="file" onchange="this.form.submit();" />
<% end %>
謝謝,我在2周前開始在rails中編寫代碼,並且不知道.object,現在非常簡單! – David 2010-07-16 12:47:52