[英]How can I create multiple objects in a single form in Rails 3.1?
I have a Photo model and I want to set up a form so that a user can create multiple photos from the same form. 我有一个Photo模型,我想设置一个表单,以便用户可以从同一个表单创建多个照片。 I've watched the Railscasts #196 and 197 on nested model forms but that is more complex than what I need and deals more with forms containing multiple models, not multiple objects of the same model.
我在嵌套模型表单上观看了Railscast#196和197,但这比我需要的更复杂,并且更多地处理包含多个模型的表单,而不是同一模型的多个对象。 Below is my code for a simple form that lets a user attach an image and create a new Photo object.
下面是我的一个简单表单的代码,它允许用户附加图像并创建一个新的Photo对象。 I've experimented with fields_for and tried nesting but it seems overly complicated and I can't get it working.
我已经尝试过fields_for并尝试嵌套,但它似乎过于复杂,我无法让它工作。 Any ideas on how I could set this form up to allow the user to attach 5 images to create 5 new Photo objects?
有关如何设置此表单以允许用户附加5个图像以创建5个新Photo对象的任何想法?
<%= form_for(@photo, :html => { :class => "form-stacked", :multipart => "true" } ) do |f| %>
<div class="clearfix">
<%= f.file_field :image %>
</div>
<div><%= f.submit "Upload", :class => "btn primary" %></div>
<%end %>
I ended up using plupload for the multiple file (photo) uploads with carrierwave. 我最终使用plupload用于使用carrierwave上传多个文件(照片)。 I used the plupload-rails gem and the following code and all works well:
我使用了plupload-rails gem和以下代码,一切运行良好:
<%= javascript_tag do %>
$(function(){
var uploader = $('#uploader').pluploadQueue({
runtimes : "html5, gears,flash,silverlight,browserplus",
max_image_size : '100mb',
url : "/photos",
unique_names : true,
multipart: true,
preinit: attachCallbacks,
multipart_params: {
"authenticity_token" : '<%= form_authenticity_token %>'
},
flash_swf_url : '/plupload/js/plupload.flash.swf',
silverlight_xap_url : '/plupload/js/plupload.silverlight.xap'
}); //end initial script
//redirect after complete
function attachCallbacks(uploader) {
uploader.bind('FileUploaded', function(Up, File, Response) {
if((uploader.total.uploaded + 1) == uploader.files.length){
var filesAdded = uploader.files.length;
window.location = "<%=j edit_individual_photos_path %>"+ '?files=' + filesAdded;
}
});
}
}); //end function
<% end %>
<div class="" id="uploader"></div>
Hope this helps someone get going. 希望这有助于某人开始。
I like Ryan Bates Nested_Form gem, because I'm lazy. 我喜欢Ryan Bates Nested_Form的宝石,因为我很懒。 But I'd do this
但我会这样做
<%= 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 %>
Then a _photo.erb partial 然后是_photo.erb偏
<div class="clearfix">
<%= f.file_field :image %>
</div>
In regards to your comment: 关于你的评论:
I think this is what you are looking for, I did it for you here (based on Railscast carrierwave episode): 我想这就是你要找的东西,我在这里为你做了(基于Railscast载波集节目):
https://github.com/rbirnie/image-upload https://github.com/rbirnie/image-upload
Basic source: 基本来源:
Gallery Model 画廊模型
class Gallery < ActiveRecord::Base
attr_accessible :name, :paintings_attributes
has_many :paintings
accepts_nested_attributes_for :paintings
end
Paintings model: 绘画模型:
class Painting < ActiveRecord::Base
attr_accessible :gallery_id, :name, :image, :remote_image_url
belongs_to :gallery
mount_uploader :image, ImageUploader
end
Galleries edit 画廊编辑
<%= 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 %>
~ 〜
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.