一次向Carrierwave(HTML5)上传多个文件到Rails应用 [英] Uploading multiple files at once to Rails app with Carrierwave (HTML5)
问题描述
我很接近...非常接近...我能够上传单个文件就好...但是当我改变我的表单的类型 file_field
到:multiple =>真正的
,所以我可以一次上传多个图像,我上传的文件被包装在一个数组中...和'accep_nested_attributes_for`的'魔术'丢失。
编辑:
经过更多的检查后,我想知道是否需要打扰 accepts_nested_attributes_for
?也许我应该只有一个 file_field,:multiple =>在我的画廊窗体(而不是一个嵌套窗体),然后在我的创建操作创建新的画廊,然后通过
可以这么说,为每个元素调用 params [:gallery]中的每个元素循环[真实
:photos_attributes] [0] [:image] @ gallery.photos.create
。 ?!?看起来很麻烦...但是这是我所能想出来的。
希望有更多的Rails经验的人可以参加...
参数:
$ p $ {utf8=>✓,
authenticity_token =>9jXvIwcllct7UyUfo6cvhEucQf2u3SY50SuaCLtFO4c =,
gallery=> {
name=>First Gallery,
photos_attributes=> {0= > {
image=> [
#< ActionDispatch :: Http :: UploadedFile:0x00000104b78978
@ original_filename =first_test_image.jpg,
@ content_type = image / jpeg,
@ headers =Content-Disposition:form-data; name = \gallery [photos_attributes] [0] [image] [] \; filename = \first_test_image。 jpg \\r\\\
Content-Type:image / jpeg\r\\\
,
@tempfile =#< File:/ var / folders / bQ / bQYZC2ukFZCvbKzEDGRtJE +++ TI / - Tmp- / RackMultipart20110622-4459-vz78ee>> ;,
#< ActionDispatch :: Http :: UploadedFile:0x00000104b78950
@ original_filename =second_test_image.jpg,
@ content_type =image / jpeg,
@ headers =Content-Disposition:form-data;名称= \ 画廊[photos_attributes] [0] [图像] [] \; filename = \second_test_image.jpg \\r\\\
Content-Type:image / jpeg\r\\\
,
@tempfile =#< File:/ var / folders / bQ / bQYZC2ukFZCvbKzEDGRtJE +++ TI / -Tmp- / RackMultipart20110622-4459-1jzhhyg>>
]
}
}
},commit=>保存, action=>create,controller=>galleries}
#app / models / gallery.rb
class Gallery< ; ActiveRecord :: Base
has_many:photos,:dependent =>:destroy
accepting_attributes_for:photos
end
$ b#app / models / photo.rb
class Photo< ActiveRecord :: Base
belongs_to:gallery
mount_uploader:photo,PhotoUploader
end
#config / routes.rb
资源:画廊做
资源:照片,只:=> [:创建,:销毁]
结束
GalleriesController
def new
@gallery = Gallery.new
@gallery .photos.build
respond_t o do | format |
format.html#new.html.erb
format.json {render json:@gallery}
end
end
...
def创建
@gallery = Gallery.new(params [:gallery])
respond_to do | format |
if @ gallery.save
format.html {redirect_to @gallery,注意:'Gallery was successfully created。'}
format.json {render json:@gallery,status::created,位置:@gallery}
else
format.html {render action:new}
format.json {render json:@ gallery.errors,status::unprocessable_entity}
end
end
end
你可以对params数组做一些修改,例如:
aux = []
params [:gallery ] [:photos_attributes] [:image] .each do | f |
aux<< {:image => f}
end
params [:post] [:photos_attributes] = aux
$ b @gallery = Gallery.new(params [:gallery])
我有一个类似的问题,我知道这是一个丑陋的黑客攻击,但它适用于我。
I'm close...very close...I'm able to upload single files just fine...but when i change the type of my form's file_field
to :multiple => true
so I can upload multiple images at once my uploaded files get wrapped in an array...and the 'magic' of 'accepts_nested_attributes_for` is lost.
Edit:
After more examination I wonder if I even need to bother with accepts_nested_attributes_for
? Perhaps I should just have a file_field, :multiple => true
in my Gallery form (as opposed to a nested form) and then in my create action create the new gallery and then loop through each element in the params[:gallery][:photos_attributes]["0"][:image]
array by hand, so to speak, calling @gallery.photos.create
for each element. ?!? Seems cumbersome...but it's all I can come up with.
Hoping someone with more experience with Rails can chime in...
Params:
{"utf8"=>"✓",
"authenticity_token"=>"9jXvIwcllct7UyUfo6cvhEucQf2u3SY50SuaCLtFO4c=",
"gallery"=>{
"name"=>"First Gallery",
"photos_attributes"=>{"0"=>{
"image"=>[
#<ActionDispatch::Http::UploadedFile:0x00000104b78978
@original_filename="first_test_image.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"first_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-vz78ee>>,
#<ActionDispatch::Http::UploadedFile:0x00000104b78950
@original_filename="second_test_image.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"second_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-1jzhhyg>>
]
}
}
}, "commit"=>"Save", "action"=>"create", "controller"=>"galleries"}
#app/models/gallery.rb
class Gallery < ActiveRecord::Base
has_many :photos, :dependent => :destroy
accepts_nested_attributes_for :photos
end
#app/models/photo.rb
class Photo < ActiveRecord::Base
belongs_to :gallery
mount_uploader :photo, PhotoUploader
end
#config/routes.rb
resources :galleries do
resources :photo, :only => [:create, :destroy]
end
GalleriesController
def new
@gallery = Gallery.new
@gallery.photos.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @gallery }
end
end
...
def create
@gallery = Gallery.new(params[:gallery])
respond_to do |format|
if @gallery.save
format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' }
format.json { render json: @gallery, status: :created, location: @gallery }
else
format.html { render action: "new" }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
You can do a little hack for the params array, something like:
aux = []
params[:gallery][:photos_attributes][:image].each do |f|
aux << {:image => f}
end
params[:post][:photos_attributes] = aux
@gallery = Gallery.new(params[:gallery])
I have a similar problem, I know it's an ugly hack, but it works for me.
这篇关于一次向Carrierwave(HTML5)上传多个文件到Rails应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!