我喜欢瑞恩贝茨Nested_Form宝石,因为我很懒。但我会做到这一点
<%= semantic_nested_form_for @user, :html => {:multipart => true} do |f| %>
<%= f.fields_for :photo %>
<p><%= f.link_to_add "Add Photo", :photo %></p>
<div class="actions">
<%= f.submit :class => "btn-success" %>
</div>
<% end %>
然后局部
<div class="clearfix">
<%= f.file_field :image %>
</div>
在问候您的留言_photo.erb:
我认为这是你在找什么,我是做你在这里(基于Railscast carrierwave插曲):
https://github.com/rbirnie/image-upload
基本来源:
图库型号
class Gallery < ActiveRecord::Base
attr_accessible :name, :paintings_attributes
has_many :paintings
accepts_nested_attributes_for :paintings
end
绘画模式:
class Painting < ActiveRecord::Base
attr_accessible :gallery_id, :name, :image, :remote_image_url
belongs_to :gallery
mount_uploader :image, ImageUploader
end
画廊编辑
<%= nested_form_for @gallery, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :paintings do |photo_form| %>
<%= photo_form.label :name %>
<%= photo_form.text_field :name %>
<%= photo_form.file_field :image %>
<%= photo_form.link_to_remove "Remove this photo" %>
<% end %>
<p><%= f.link_to_add "Add a photo", :paintings %></p>
<p><%= f.submit %></p>
<% end %>
〜
我不确定这是我在找什么..我正在玩弄它一点点,无法让它工作。基本上,我想对两种不同的模型执行两种不同的操作:我想更新has_many照片的用户模型,并且我想创建两个新的照片对象。在使用您的代码时,我正在更新用户和现有照片,而不是创建新的Photo.rb对象 – kcurtin 2012-02-16 11:42:32