我有一个页面,我可以查看产品,并直接从此页面用户可以添加和删除与产品相关联的多个图像。我在表单中使用回形针上传新文件。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