#253 CarrierWave File Uploads
CarrierWave makes it easy and clean to add file uploads to your application. It supports a variety of ORMs, frameworks and image processors.
- Download:
- source codeProject Files in Zip (480 KB)
- mp4Full Size H.264 Video (21 MB)
- m4vSmaller H.264 Video (11.7 MB)
- webmFull Size VP8 Video (24.9 MB)
- ogvFull Size Theora Video (25.4 MB)
You have a scary way of posting videos on exactly what I need just a few minutes after I start struggling with it. Keep up the good work!
I'd like to recommend handling images on the fly with Dragonfly https://github.com/markevans/dragonfly/
Maybe an episode one day...?
Great post !
But I got error in show.html.erb, it said "wrong number of argument 1 of 0). Then I changed
<%= image_tag @user.avatar(:thumb.)to_s %> to
<%= image_tag @user.avatar.thumb.url.to_s %>
it turned ok.
Correct. Ryan has a typo in his code...it should be:
<%= image_tag painting.image.url(:thumb).to_s %>
NOT: <%= image_tag painting.image_url(:thumb).to_s %>
Related Post On Stack Overflow
Thanks for this cast !
What textmate shortcut did you use to autocomplete "field"?
Thanks for this screencast!
Please, do another wih multiple file uploads,like:
swfupload.org
uploadify.com
plupload.com
I'm using CarrierWave to process images and store them on S3. It's a great gem.
@Tim the reason for that is you need to specify the _url part before the symbol. So in your case, @user.avatar_url(:thumb).to_s would be correct.
Pleased to see a flexible uploading solution with as much polish as Paperclip.
Apparently, both Phil and Ryan are artists in their respective domains. Keep on the good work, both of you!
The only bad thing I've found in CarrierWave is that it doesn't have some built-in validations, e.g upload size =>
http://stackoverflow.com/questions/4753066/validate-size-of-upload-using-carrierwave
@7: CarrierWave has built in support for MiniMagick, which does the same thing Paperclip does: shelling out to ImageMagick directly. I'd really recommend taking another look at RMagick though, it's far more stable these days, I've never had a problem with it.
@14 Juan: IIRC validates_size_of works with CarrierWave at least on ActiveRecord and Mongoid.
Awesome!
A couple of suggestions for future episodes:
1. Converting from Paperclip to CarrierWave
2. Uploading images with CarrierWave, ajax, & an upload status bar -- include how to do this with HTML5 drag&drop and I'll weep great tears of joy.
I subscribe this suggestions ;)
Will be very helpfull.
Good suggestion! :D
+1 from me
Agree on all!
+1
+1
Really nice!
It looks more flexible than Paperclip, though I have some problems with conversion and cropping a picture.
@15 Jonas thanks!
Good information, but polymorphic paperclip with STI is working wonders for me already.
I having an issue trying to get the (:thumb) part to work. I think it has to do with RMagick and ImageMagick because its that steps that breaks on me.
I've followed the steps exactly, the only thing i couldnt do with the steps is install ImageMagick. I followed these instructions http://www.imagemagick.org/script/install-source.php?ImageMagick=9s6tlikqbn5eb0u0spg5o38sa4#unix
The error I'm getting http://pastie.org/1564737
would be great to have a carrierwave + jcrop integration railscast, as per paperclip cropping episode
I second /users/858's request for a multiple-files screencast. I've been working on a SWFUpload implementation with Paperclip and S3 for a few weeks now, without much success... I got bogged down with the JavaScripts for making a simple Gmail-like uploads.
I'm wondering what the cleanest way of adding custom validations to a Uploader is. Of corse you can add a validation to your AR model, but that feels kind of messy. In my use case I need to validate the content type (not only the file extension).
Any ideas how to do that?
There seems to be a [BUG] with RMagick and ruby 1.9.2 when trying to resize... I finall found the solution here if you still want to use Rmagick...
However, I found it much easier to just replace RMagick with MiniMagick and everything works great. MiniMagick uses less system resources too.
Had the same issue and used the mini_magick gem and all is working fine now. Thanks for pointing that out.
Henrik is right. Ryan does have a way of posting videos on exactly what I need. I believe Ryan is out best evidence for the paranormal to date :). From the paintings, it looks like talent runs in the family!
And your dad is an awesome artist.
Is there anyway to integrate Jcrop to show/use a certain part of the image for the the thumbnails .. like facebook
Who has two thumbs and loves Ryanb.... This guy!
Thanks for another great Railscast! I also love your nifty-generators.
CarrierWave throws a "400 Bad Request" error when using remote_image_url with a value of
http://ecx.images-amazon.com/images/I/51YBrr%2B0tvL.jpg
I believe it has to do with the '%' in the image name.
Any ideas on how to get around this?
High fives to you kind sir.
Being a supreme rails noob, and wanting to leanr more, I put together a sample app demonstrating carrierwave + nested forms here
github.com/galaxylord/carrierwave_nested
Maybe I've missed something, but is it normal to download all assets from remote storage (s3) just to determine file size? For example I have 100 videos on production and have just migrated to carrierwave. Files already on S3. So all my production instanced need to have local copies just to display `video.source.size`???
it is according to https://github.com/jnicklas/carrierwave/blob/c9b5cbbe03ba8aca3977/lib/carrierwave/uploader/proxy.rb#L56
Very large data files 50MB
Thanks for the wonderfully presented railscast.
I am wanting to upload mp3 files to my server then add mp3 metadata and zip them up before sending them off to Amazon S3.
I am concerned about the upload time so I was going to use Nginx upload module.
Another option would be to use Carrier wave and then store the files at Amazon then retrieve them for the processing and return them separately.
I would appreciate some reflections even a suggestion where to post such a question.
Thanks again
Best wishes Russell
Carierwave isn't so new ... I used it first time something about year ago for storing files in MongoDB GridFS. And yes, carrierwave is really great and well documented.
Yuval Tobias and others interested in multiple fileupload to s3 with progress bar: check out my fancyupload based S3 uploader: http://www.railstoolkit.com/posts/rails-amazon-s3-uploader-with-progress-bar-2010-update
@Nico Ritsche
Great gem thanks!!
But i don't use S3, do you know any gem to multiple file upload in easy way?
@user 858: You could try uploadify: http://www.uploadify.com/
@Nico Ritsche
I know uploadify (and swfupload, pupload...) the difficult is to integrate with rails !
How would I add a restriction on the file size?
Here's a little migration tip for tose still on attachment_fu :
http://ruby.simapse.com/2011/03/migrate-attachmentfu-to-carrierwave.html
Hi Charly, Thank you for this comment, I know you posted this almost a year a go, but just one question, do you still remember how can we use that module, the article just post the module, but there are no instructions about it. Do you have the any idea about how? Thank you very much
My local server keeps crashing whenever I try and use a rmagick command in CarrierWave.
I've just installed ImageMagic. I'm pretty sure it should be working fine. The "convert logo: logo.gif" "display logo.gif" test command suggested by rmagick work in my terminal.
When I try and create a thumb version with "process :resize_to_limit," Rails crashes and I get a message saying "You may have encountered a bug in the Ruby interpreter or extension libraries"
Any thoughts on what might be causing the crash?
Found a solution (kinda). I guess ImageMagic has some problems working with Ruby 1.9
http://stackoverflow.com/questions/2838307/why-is-this-rmagick-call-generating-a-segmentation-fault
Once again a great vidcast.
The one about the token fields blew my mind.
I would suggest to make another one about multiple uploads. Since it's damn hard ( for a noob like my at least ) to get it working.
Thanks again for your great work and effort.
Iam using active record, and the validates_size_of, only checks the length of the name of the file, not the actual size.
I got a big crash when I tried to resize to the thumbnail... I'm running RMagick 2.13.1 with ImageMagick 6.6.2-10:
~/.rvm/gems/ruby-1.9.2-p136/gems/carrierwave-0.5.3/lib/carrierwave/processing/rmagick.rb:248: [BUG] Segmentation fault
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.7.0]
...
Will try MiniMagick
I had a big crash too. I used this configuration:
ImageMagick 6.6.7-0 2011-11-26
rmagick (2.13.1)
on rvm 1.8.5 Ruby Version: 1.9.2, Rails 3.1.1
Using rake (0.9.2.2)
Using multi_json (1.0.3)
Using activesupport (3.1.1)
Using builder (3.0.0)
Using i18n (0.6.0)
Using activemodel (3.1.1)
Using erubis (2.7.0)
Using rack (1.3.5)
Using rack-cache (1.1)
Using rack-mount (0.8.3)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.0.3)
Using actionpack (3.1.1)
Using mime-types (1.17.2)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.3.0)
Using actionmailer (3.1.1)
Using bcrypt-ruby (3.0.1)
Using orm_adapter (0.0.5)
Using warden (1.1.0)
Using devise (1.5.1)
Using fastercsv (1.5.4)
Using formtastic (1.2.4)
Using has_scope (0.5.1)
Using responders (0.6.4)
Using inherited_resources (1.2.2)
Using arel (2.2.1)
Using tzinfo (0.3.31)
Using activerecord (3.1.1)
Using activeresource (3.1.1)
Using bundler (1.0.21)
Using rack-ssl (1.3.2)
Using json (1.6.1)
Using rdoc (3.11)
Using thor (0.14.6)
Using railties (3.1.1)
Using rails (3.1.1)
Using kaminari (0.12.4)
Using polyamorous (0.5.0)
Using meta_search (1.1.1)
Using sass (3.1.10)
Using activeadmin (0.3.4)
Using ansi (1.4.1)
Using carrierwave (0.5.8)
Using coffee-script-source (1.1.3)
Using execjs (1.2.9)
Using coffee-script (2.2.0)
Using coffee-rails (3.1.1)
Installing jquery-rails (1.0.19)
Using rmagick (2.13.1)
Using sass-rails (3.1.5)
Using simple_form (1.5.2)
Using sqlite3 (1.3.4)
Using turn (0.8.3)
Using uglifier (1.1.0)
So got it: The Installation of Imagemagick an RMagick was incorrect. Installed the actual Version of xcode, uninstalled imagemagick (had several 6.xx Versions around) and uninstalled rmagick. Installed everything new and did this:
brew remove imagemagick
rm -rf 'brew --cache imagemagick'
brew install -f imagemagick --disable-openmp
works fine.
Thank you so much!!!
I also had troubles with RMagick, but when I replaced
gem "rmagick"
with
gem "mini_magick"
in the Gemfile
and
include CarrierWave::RMagick
with
include CarrierWave::MiniMagick
in /uploaders/image_uploader.rb
everything worked great!
This worked for me as well, thanks! :)
Is there any way to get image dimensions in my view?
@user.avatar_width(:thumb)
or@user.avatar_height(:thumb)
Something like this?
I'm wondering the same thing...saw you've asked this in a couple places on the web. Did you find an answer yet?
I'm stack with this too. Any success?
Please see this project on github:
https://github.com/mdchaney/CarrierWave-ImageSize
It does what you need pretty easily. Just stick it in your lib directory and then use it from your uploader by first loading the file:
require 'carrierwave/image_size'
and then including it in the class:
include CarrierWave::ImageSize
You need to add another field to your table to keep the extra info. If your picture is "avatar", you add "avatar_information" as a wide varchar field. It will then store size information for your image and various versions, as well as a content type. You then use "@user.avatar.image_width", "@user.avatar.thumb.image_height", etc. to get the size. It adds "image_width", "image_height", and "content_type" methods to all versions. It also works with nested versions.
It does require ImageMagick, specifically the "identify" command.
It would be a good upgrade to use MiniMagick or RMagick to get the size. It would also be nice for this to be turned into a gem. I just don't have time, but I am using this code on multiple web sites.
Hope that helps.
Is it possible to have a nested model solution, for example, where each painting requires that you upload three images instead of just one, and then stores them in an Images table with a painting_id column and gallery_id column, instead of just an images column in the Paintings table?
Im trying to do this and having trouble getting the logic down.
Awesome screencast, maybe you could expand with a multiple upload one :)
See this tutorial i think it will help:
http://blog.assimov.net/post/4306595758/multi-file-upload-with-uploadify-and-carrierwave-on
Don't use
model.image.to_s
but insteadmodel.image.url
Using to_s breaks the default_url override if you want to use a default image when none exists.
This guy:
Is it possible for CarrierWave to generate URL for downloading files from S3 which will expire in certain of time? I'm having difficulty in preventing users from downloading files if some conditions met.
Indeed I gave it a try and I found it awesome! thanks @jnicklas, @rmagick and of course @ryanb
Really nice screencast Ryan. Could you make a screencast for multiple file upload in Rails 3.1? It's so confusing for a lot of people ( myself included )!
I have a blog application and would like to default to an image beaded on the category association of the post if no image is uploaded. Is this possible?
in case your struggling the beaded should be 'based';
what I want to know is if no image is uploaded for a post can you default to the image of the category that the post belongs to?
Paul,
I know this is an old post, did you ever find a solution to this? I'm trying to do the same thing.
Stupid question, but does anyone got any chance at removing images ?
Supposedly there is some way by adding a checkbox named :remove_image or whatever your image is called.
But it doesn't seem to work. Could someone help me ?
Thanks a lot!
If your using
attr_accesible
on your model make sure:remove_image
is set there, and it should workHey guys,
I spent about 6 hours stuck on getting Rmagick to work on OSX 10.6 and wanted to share my solution to maybe help at least somebody out there. Pretty much, I used homebrew to install Imagemagick but with a special way to avoid a bug I kept running into: http://stackoverflow.com/questions/2838307/why-is-this-rmagick-call-generating-a-segmentation-fault/8614289#8614289
(if you're using Macports, I feel sorry for you; definitely use Homebrew. Life is much, much easier)
Also then install the Rmagick gem. And things should be good to go.
Thanks Ryan for the great Railscasts as always.
Much thanks to those of you who found the imagemagick ruby 1.9.2 conflict and posted an answer here. Used:
brew install -f imagemagick --disable-openmp
and everything is happen again...
The following post describes a solution for image uploading using CarrierWave while image transformations are done seamlessly in the cloud. Uploaded images are stored in the cloud and delivered through a CDN.
No need to install RMagick, MiniMagick and ImageMagick.
http://cloudinary.com/blog/ruby_on_rails_image_uploads_with_carrierwave_and_cloudinary
Just a heads up. Cloudinary is expensive.
Thanks for this episode
(the link to your Dad's website doesn't work)
I have followed the steps suggested in the video to save a csv file and while I am not getting an error, the file is not saved by the uploader. Are there any common causes for the file not being saved that I should look into?
I am using this gen with active admin. I want to be able to save the file/image to a postgres DB ... the data types in the DB are all set as bytea.
How can I achieve this?
So after some time on google I bump into this. Am trying it, it saves to postgresql DB.
http://www.rubydoc.info/gems/carrierwave-postgresql/frames
Will update once I get it working fine.
Hey Ryan,
First of all thanks for all your railscasts, they are fantasticly helpfull and well done.
I followed this one but all my uploads are very slow in any config I use (:fog, :s3)
I am on an EC2 medium instance and using s3 in same region.
Any other upload to the instance works at normal speed (I also upload zip files for other purposes). It's really when using carrierwave that the slowness appears.
If I save a user without avatar, it saves fast.
As soon as I upload an image (40k or 400k), it takes about 6/7 seconds to upload and generate 2 smaller versions thumbnails.
I tried to remove thumb creation, same thing (6/7 seconds)
Any clue what can be happening here ?
(Rails 3.1.10, carrierwave 0.5.8, fog 1.3.1)
Unfortunatly I can not use a backgrounder and carrierwave_direct seems more tricky to use in my case (all has to happen in same new/edit form)
Thanks a lot
Phil
please mate how do i add a default image, maybe when new user logged in he can see it ?
please mate how do i add a default image, maybe when new user logged in he can see it ?
Use model.image.url
Using to_s breaks the default_url.
This guy: (in the Uploader) is where you set the default image.
Hey, 1st I love the Railscasts series. You're a gifted instructor. Thanks!!
2nd, for one of our apps we needed to use Oracle with active record. I assume I can use Carrier Wave the same way, as if it were just a simple rails with mysql set up w/o any difference. Is that correct? If it's different, can you comment on how it's different? Thanks.
Has anyone been able to get CarrierWave and RMagick to work on jRuby?
My error: uninitialized constant Magick::ImageMagickError
Same error with RMagick4J...
Note to all using FriendlyId for pretty URLs. In any view to create an new image be sure to change
<%= link_to "New Image", new_image_path(:x_id => @x) %>
to<%= link_to "New Image", new_image_path(:x_id => @x.id) %>
or else you spend countless hours trying to figure out why it just won't work.Thanks, Ryan! This was great!
If you are having Error with default thumbnails (the nil error) consider this code:
def filename
super.chomp(File.extname(super)) + '.png' unless super.nil?
end
Sadly, I am getting ActiveModel::ForbiddenAttributesError when I try to use this recipe in Rails 4. Any idea how to update it for Rails 4?
Make sure you add image or whatever you added in the migration to your params in whatever controller you added it to.
Is there a way to add multiple images under one model?
How to delete an image? I see people are using :remove_image check box, but is there any other way?
Thanks
Is there a way to generate the different versions for the default_url? Ideally, I'd like this to match the versions defined in the uploader.
You can get the requested version using
version_name
method, so you can just add to the name of the default file nameI have a crash in mac that I resolve doing this in my Gemfile:
gem 'rmagick', :require => 'RMagick'
Oh my god Thanks you saved my application :) I had issues with stack level too deep. I didn't know what to do with my RMagick. Thanks a lot!!!!
that worked..thanks!!
If
fails, make sure to check out this stack
overflow, it worked for me (OSX)
http://stackoverflow.com/questions/19274293/error-installing-rmagick-error-failed-to-build-gem-native-extension
which requires the installation of ImageMagick beforehand
how to upload multiple files using carrierwave
hi
i am using carrierwave gem for file upload. It is working fine with single upload, but how to upload multiple files please help me.
This is my form:
attachment_uploader.rb:
I used http://richonrails.com/articles/allowing-file-uploads-with-carrierwave for refernce.
Thanks a bunch bruv! Was pissing about with paperclip last week with no joy.m Subscribed this morning and got it working!
hi
thanks for this awesome post
i use carrierwave for uploading and i have problem to upload image and video in one uploader
i can do that separately, but when i use include CarrierWave::Video & include CarrierWave::RMagick in my uploader, video not upload and i recive this error "Failed to manipulate with rmagick, maybe it is not an image?
any idea?
Wow! Bates, I always admired the quality of your videos and your work, this defines the personality and your dad seems to be a great person too!
Sincerely
Gabriel Francisco
in the painting controller use the following:
Validation failed: Image could not download file: 301 Moved Permanently (Invalid Location URI)
this is my code
url = URI.extract(URI.encode((product.at_xpath('picture').text.strip)))[0]
this url i am get
"http://trendsbrands.ru/upload/thumbs/%D0%A3%D0%A2000005993/e916578dfbf37f1d03c2f047dcedb986/middle.jpg"
any help or idea what going on