我要寻找一个解决方案,让用户通过一个是,file_field上传多张图片的能力。我特地到选项,例如jQuery的文件上传和Uploadify,但还没有遇到很好的例子有一个有效的解决方案。
I am looking for a solution to give the user the ability to upload multiple images through one file_field. I have looked in to options such a Jquery File Upload and Uploadify but have yet to come across good examples with a working solution.
我已经有多个图像设置,
I already have multiple images setup,
has_attached_file :asset,
:styles => { :large => "640x480", :medium => "300x300", :thumb => "100x100" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:contributor_id/:listing_name/:filename"
现在我显示5个人file_fields
Right now I am displaying 5 individual file_fields
def new
@listing = Listing.new
5.times {@listing.assets.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: @listing }
end
end
我想有
<%= f.file_field :asset, :multiple => true %>
这允许用户选择在他们的文件浏览多个文件。但我怎么能处理这些与嵌套模式?并让他们上传。
That allows the user to select multiple files in their file browser. But how can I process these with a nested model? And get them to upload.
因此,有一些问题在这里。
So there are a few issues here.
首先,回形针的 has_attached_file
的方法是不是关联到多个文件。它看起来像你试图建立一个资产,如果它是一个Rails的关联。所有的回形针也就是把几个字段到表来存储有关文件的一些元数据,你会得到每声明 has_attached_file
中的一个附加的文件。如果您要附加5个文件,你需要做的是这样的:
First, Paperclip's has_attached_file
method isn't an association to many files. It looks like you're trying to build an "asset" as if it's a Rails association. All Paperclip does is put a couple of fields into your table to store some meta-data about the file and you get one attached file per declaration of has_attached_file
. If you want to attach 5 files, you would need to do something like:
has_attached_file :asset1
has_attached_file :asset2
has_attached_file :asset3
has_attached_file :asset4
has_attached_file :asset5
,或者,你可以创建另一个模型只是存储文件。例如:
OR, alternatively, you could create another model just to store the files. For example:
class Listing < ActiveRecord::Base
has_many :assets
end
class Asset < ActiveRecord::Base
belongs_to :listing
has_attached_file :picture
end
这样的话,你可能会连接到一个房源的多个资产(你没有说什么,原来的目标是,所以我只是把它叫做上市)。
That way, you could have multiple assets attached to one listing (you didn't say what the original object was so I just called it "listing").
二,有没有这样的东西作为一个多文件上传的HTML(,正因为如此,在的是,file_field
方法并不需要 :多=&GT;真
参数你将不得不使用一些超越内置的,如果你想多文件上传表单处理的Rails Uploadify是一个不错的选择(我以前使用过)。有是一个宝石,将改变文件中的字段使用uploadify(并支持:多=&GT;真
语法,你想要的):https://github.com/mateomurphy/uploadify_rails3/wiki.但是,我不能担保它有多好。
Second, there is no such thing as a multiple file upload in HTML (and, as such, the file_field
method doesn't take a :multiple => true
argument. You'll have to use something beyond Rails built-in form handling if you want multiple-file upload. Uploadify is a decent choice (that I've used before). There is a gem that will transform file fields to use uploadify (and will support the :multiple => true
syntax that you want): https://github.com/mateomurphy/uploadify_rails3/wiki. However, I cannot vouch for how good it is.
我的建议是,开始一步一步的。通过快速上传到Rails的可以是涉及到处理您的形式CSRF元标记等领域一个复杂的过程。开始通过使一种形式,其允许用户上传一个文件,并将其存储通过回形针。那么,也许打破 has_attached_file
声明成另一种模式,这样你可以有1或模型(如上图所示,在多模式code座)相关的许多文件。然后尝试加入Uploadify或另一种选择。厄尼·米勒对整合Uploadify一个体面的教程:http://erniemiller.org/2010/07/09/uploadify-and-rails-3/.
My advice would be to start step-by-step. Uploading via Flash to Rails can be a complicated process that involves dealing with the CSRF meta-tag and other fields in your form. Start by making a form that allows a user to upload one file and stores it through Paperclip. Then maybe break the has_attached_file
declaration into another model so that you can have 1 or many files associated with a model (as shown in the multi-model code block above). Then try adding Uploadify or another alternative. Ernie Miller has a decent tutorial on integrating Uploadify: http://erniemiller.org/2010/07/09/uploadify-and-rails-3/.
要开始,请记住, has_attached_file
只能附加的一个的文件。当您尝试调用 @ listing.assets
不存在资产。还有的的的资产。你需要自己创建一个单独的模型,并用Rails的协会,如果你想多个文件。
To start, remember that has_attached_file
can only attach one file. When you try calling @listing.assets
there is no "assets". There is an asset. You need to create a separate model yourself and use Rails' associations if you want multiple files.