How can I create multiple objects in a single form in Rails 3.1?

允我心安 提交于 2019-12-10 10:09:42

问题


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. 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. Below is my code for a simple form that lets a user attach an image and create a new Photo object. I've experimented with fields_for and tried nesting but it seems overly complicated and I can't get it working. Any ideas on how I could set this form up to allow the user to attach 5 images to create 5 new Photo objects?

<%= 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 %>

回答1:


I ended up using plupload for the multiple file (photo) uploads with carrierwave. I used the plupload-rails gem and the following code and all works well:

<%= 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.




回答2:


I like Ryan Bates Nested_Form gem, because I'm lazy. 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

<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):

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 %>

~



来源:https://stackoverflow.com/questions/9298325/how-can-i-create-multiple-objects-in-a-single-form-in-rails-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!