我正在尝试在我的 Rails 应用程序中使用 ajax 图片上传功能。我正在使用 Paperclip 进行正常的图像上传,并且工作正常,但我似乎无法连接 ajax 方法。我正在使用 Rack RawUpload 和 File Uploader插入。我遵循了一般说明 here ,但我一直停留在实际将图像附加到创建时的新对象上。这是我的 Controller 代码:
@bottle = Bottle.new(params[:bottle])
@bottle.user_id = current_user.id
#file = params[:qqfile].is_a?(ActionDispatch::Http::UploadedFile) ? params[:qqfile] : params[:file]
is_qq = params.has_key?(:qqfile)
if is_qq
params[:bottle][:image] = params.delete(:file)
render :json => { "success" => true }
else
respond_to do |format|
if @bottle.save
format.html { redirect_to '/bottles', notice: 'Bottle was successfully created.' }
format.json { render json: @bottle, status: :created, location: @bottle }
else
format.html { render action: "new" }
format.json { render json: @bottle.errors, status: :unprocessable_entity }
end
end
end
这是查看代码:
<%= simple_form_for(@bottle, :html => { :multipart => true }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :brand, :placeholder => 'Brand', :label => false %>
<%= f.input :region, :placeholder => 'Region', :label => false %>
<%= f.input :age, :collection => 5..35, :prompt => "Bottle Age", :label => false %>
<%= f.input :price, :placeholder => 'Price', :label => false, :as => :currency, :input_html => { :class => 'span2' } %>
<%= f.input :image_id, :as => :hidden %>
<%= f.text_area :notes, :placeholder => 'Tasting notes...', :size => "160x5" %>
</div>
</div>
<div class="span3 offset2">
Drag a file from your desktop here...
<div class="well" height="105" style="width:200px;height:300px;">
<!-- <img src="http://placehold.it/200x300" alt="" class="temp_image"> -->
<div id="file-uploader"></div>
</div>
or...
<%= f.file_field :image %>
</div>
<div class="span8 offset2">
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to bottles_path, :class => 'btn btn-danger' do %>
Cancel
<% end %>
</div>
<% end %>
我像这样使用文件 uploader 上传:
var uploader = new qq.FileUploader({
debug: false,
/* Do not use the jQuery selector here */
element: document.getElementById("file-uploader"),
action: '/bottles',
allowedExtensions: ["jpg", "png"],
/*
* This uploads via browser memory. 1 MB example.
*/
sizeLimit: 1048576,
/* Set Article category on submit */
onSubmit: function(id, fileName) {
uploader.setParams({
authenticity_token: $("input[name='authenticity_token']").attr("value")
});
},
onComplete: function(id, fileName, responseJSON){
url = responseJSON.image.image.url;
$('.well').html('<img src="'+url+'" />');
$('input#bottle_image_id').val(responseJSON.image.id);
}
});
它似乎可以使用 Rack 上传,并将 :file 参数传递给该方法,但我无法将 param[:bottle][:image] 分配给 param[:file],我收到错误消息:
undefined method `[]=' for nil:NilClass
如有任何帮助,我们将不胜感激。
编辑:
所以我能够让 ajax 上传 Hook 到回形针上传并添加适当的参数,但现在我需要在提交表单的其余部分时更新同一个对象,而不是创建一个新对象。如何存储由 ajax 上传创建的包含所有图像内容的对象,并在提交完整表单后更新它?
编辑 2: 保存时我得到的错误是
undefined method `save' for #<ActiveSupport::HashWithIndifferentAccess:0x007ff961d08e48>
我认为这是因为我正在获取表单数据并试图将其放入同一个@bottle 对象中,但哈希值不匹配。
最佳答案
我在217行给lib/rack/request.rb打补丁解决了这个问题
替换
{}
与
@env["rack.request.form_hash"] || {}
关于ruby-on-rails - Ajax 文件上传 rails + Rack RawUpload + Paperclip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11837425/