I read this nice writeup on using Paperclip in a rails3 app to capture pictures from a webcam.
I'm trying to convert it to use Carrierwave (I use it on other apps which will all be sharing the same images, so I want to be consistent).
What I'm struggling with is how to get Carrierwave to accept the file from the flash image capture from jpegcam. Seems like Paperclip is more file oriented, and Carrierwave is more object oriented.
Here's what I'm trying:
I have a model with the carrierwave uploader mounted on it:
class Individual < ActiveRecord::Base
mou开发者_JS百科nt_uploader :picture, PictureUploader
And in the view (an "edit" view):
#webcam
= embed{:id => 'webcam_movie'}... (the embedded flash)...
= submit_tag "Take picture", :onclick => 'webcam.snap();'
= form_for @individual, :html => {:multipart => true} do |f|
= f.text_field :firstname
= f.text_field :lastname
= f.file_field :picture
= f.submit
= content_for :javascripts do
:javascript
function upload_complete(msg) {
if (msg) {
????
} else {
alert('An error occured');
webcam.reset();
}
}
webcam.set_hook('onComplete', 'upload_complete');
My problem is that Carrierwave doesn't really have an 'upload' function, rather it uses a model object. So I'm not really sure what to put in the controller to get it to accept the file. And how to process the callback in javascript. (I tried a couple of things that I'm too embarrassed to reproduce here. Got the image object to the controller, but couldn't figure out how to get it processed)
Can anyone help me out? Thanks.
I ended up fixing this with a huge hack.
In my controller I did:
def upload
@individual = Individual.find(params[:individual_id])
File.open(upload_path, 'w:ASCII-8BIT') do |f|
f.write request.raw_post
end
@individual.picture = File.open(upload_path)
if @individual.save!
render :text => "ok"
end
end
private
def upload_path # is used in upload and create
file_name = 'temp.jpg'
File.join(::Rails.root.to_s, 'public', 'uploads', file_name)
end
So I write the imiage out to a file, which carrierwave then reads in. (Of course carrierwave also makes a temp copy of the file).
This is very ugly, and probably offends all the Rails Gods, but it works.
I asked the question on the carrierwave list whether I could cut out the temp file bit and somehow just read the "request.raw_post" into the uploader object, but it's not clear that I can.
精彩评论