Ruby on Rails Image Uploading Using CarrierWave
CarrierWave is a gem that provides a simple way for you upload files from Ruby applications.
Get started with installing the latest Gem version.
Next you’ll want to add the gem to Gemfile.
The mini_magick gem is an interface between the ImageMagick program & your Ruby code. It allows you to customize your images.
Generate an uploader by running: rails generate uploader image
This should give you an image_uploader.rb file added to your models folder.
After the file is generated you want to mount the uploader by creating a column on the model you want the uploader attached to. You can achieve this by running command: rails generate add_image_to_model image:string and run rake db:migrate.
With these steps you have added an image uploading tool to use in your application.
After the uploading tool is mounted to a model, you want to secure the uploads. In order to do this, you want to specify the allowed extensions or content to allow. Do this by simply uncommented on the image_uploader.rb file as needed.
In order for images to display and be resized you can install imagemagick.
“ImageMagick is free software delivered as a ready-to-run binary distribution or as source code that you may use, copy, modify, and distribute in both open and proprietary applications”
On Mac OS X, you can use the command $ brew install imagemagick
After setup is complete, you can simply add a form that will create an image uploading tool for users.
After the form is set, you can display image on your view page using image_tag.
HOORAY! You’ve created an image uploader in your Rails application. 🎉🎉🎉