如何在 Rails 3.1 中以单一形式创建多个对象?

发布于 2025-01-05 17:46:00 字数 556 浏览 3 评论 0原文

我有一个照片模型,我想设置一个表单,以便用户可以从同一表单创建多张照片。我看过关于嵌套模型表单的 Railscasts #196 和 197,但这比我需要的更复杂,并且更多地处理包含多个模型的表单,而不是同一模型的多个对象。下面是我的一个简单表单的代码,它允许用户附加图像并创建一个新的照片对象。我已经尝试过 fields_for 并尝试嵌套,但它似乎过于复杂,我无法让它工作。关于如何设置此表单以允许用户附加 5 张图像来创建 5 个新的照片对象,有什么想法吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随梦而飞# 2025-01-12 17:46:00

我最终使用 plupload 通过 Carrierwave 进行多个文件(照片)上传。我使用了 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>

希望这可以帮助某人开始工作。

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.

小清晰的声音 2025-01-12 17:46:00

我喜欢 Ryan Bates Nested_Form gem,因为我很懒。但我会这样做

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

然后 _photo.erb 部分

<div class="clearfix">
  <%= f.file_field :image %>
</div>

关于您的评论:

我认为这就是您正在寻找的,我在这里为您做的(基于 Railscast 载波插曲):

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

~

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

~

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文