I am trying to get ajax image uploading working in my rails app. I am using Paperclip for the normal image uploading, and that works fine, but I can't seem to get the ajax method hooked up. I am using the Rack RawUpload and the File Uploader plugin. I followed the general instructions here, but I am stuck at the actually attaching the image to the new object on create. Here is my Controller code:
@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
and Here is the view code:
<%= 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 %>
I am uploading with the File Uploader like this:
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);
}
});
It seems to upload using Rack fine and it passes the :file param to the method, but I can't assign param[:bottle][:image] with the param[:file], i get error:
undefined method `[]=' for nil:NilClass
Any help would be greatly appreciated.
EDIT:
So I am able to get the ajax upload to hook into the paperclip upload and add the appropriate parameters, but now I need to update that same object when I submit the rest of the form, not create a new object. How can I store the object created by the ajax upload that contains all the image content and update it once the full form is submitted?
EDIT 2: The error i get when saving is
undefined method `save' for #<ActiveSupport::HashWithIndifferentAccess:0x007ff961d08e48>
Which I assume is because I am taking the form data and trying to put it in the same @bottle object and the hashes are not matching up.