ruby-on-rails - Ajax 文件上传 rails + Rack RawUpload + Paperclip

标签 ruby-on-rails ruby-on-rails-3 paperclip ajax-upload

我正在尝试在我的 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' %>
    &nbsp;
    <%= 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/

相关文章:

ruby-on-rails-3 - 在 ruby​​ on rails 中渲染备用 View

ruby-on-rails-3 - PG::Error: ERROR: 将记录插入关联模型时,列 "created_at"中的空值

imagemagick - 修改样式后新的回形针指纹?

ruby-on-rails - 调整回形针大小以适合矩形框

ruby-on-rails - 如何禁用回形针文件扩展名限制?

ruby-on-rails - Rails View 上的刷新按钮

ruby-on-rails - 在 Mavericks 上安装 Rails

ruby-on-rails - 如何将自定义动词(http 请求方法)添加到 rails4

mysql - Postgres 到 Mysql - 每天将数据从一个数据库传输到另一个数据库

ruby-on-rails - bundler ,Rails3 : how to avoid git check of edge gems in :test group during deploying?