[Rails] How to upload images using Carrierwave

What is carrier wave ▼

Simply put, it's a Rails library (gem) that makes it easy to add image upload functionality. You can specify the size of the image to upload.

Saved file of posted image ▼

The posted images will be accumulated under the public of the application. If you don't need to manage your images on GitHub, add a description to .gitignore as below Don't manage it with Git.

# .Add to gitignore
public/uploads/*

How to add carrierwave ▼

Execute in order from the top.

#Run in terminal
$ brew install imagemagick 
#Add to Gemfile
gem 'carrierwave'
gem 'mini_magick'
#Run in terminal
$ bundle install
#Run in terminal
$ rails g uploader image
# app/models/Post model.Edit rb
mount_uploader :image, ImageUploader
# app/uploaders/image_uploader.Edit rb
class ImageUploader < CarrierWave::Uploader::Base
  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick
  # storage :fog
#~abridgement~
  process resize_to_fit: [800, 800]
#~abridgement~
end

Commentary ▼

After running "brew install imagemagick", let's install the required gems. Add "carrierwave" and "minimagick" to the Gemfile and run bundle install from the terminal.

Next, let's create an image uploader. In the terminal, running the "rails g uploader image" command will create image_uploader.rb under the app / uploaders directory.

After creating the uploader, edit the model related to the post and describe to mount "image_uploader".

Finally, let's edit image_uploader.rb so that we can resize the image via MiniMagick. Uncomment "include CarrierWave :: MiniMagick" on line 5.

After that, add "process resize_to_fit: [vertical size, horizontal size]" to any place. (Size is arbitrary) resize_to_fit means to resize the aspect within the specified size while maintaining the aspect ratio.

Recommended Posts

[Rails] How to upload images using Carrierwave
[Rails] How to upload multiple images using Carrierwave
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
[Rails] How to delete images uploaded by carrierwave (using devise)
[rails] How to post images
[Rails] Save images using carrierwave
[Rails] How to upload images to AWS S3 using refile and refile-s3
How to download images from AWS S3 (rails, carrierwave)
[Ruby on Rails] How to use CarrierWave
How to implement image posting using rails
[Rails] How to handle data using enum
Multiple image upload function using Rails Carrierwave
[rails6.0.0] How to save images using Active Storage in wizard format
CarrierWave Upload multiple images
How to write Rails
How to uninstall Rails
[Rails] How to create a graph using lazy_high_charts
Upload multiple images easily with rails rails + carrierwave + cloudinary
How to link images using FactoryBot Active Storage
How to post images on Heroku + CarrierWave + S3
How to handle uploaded images
[Rails] How to use enum
[Rails] How to install devise
How to minimize Java images
[Rails] How to use enum
How to read rails routes
How to use rails join
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails 6] Add images to seed files (using Active Storage)
[Rails] How to implement scraping
[Rails] How to make seed
Rails learning How to implement search function using ActiveModel
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to install a decorator using gem draper
[Rails] How to use Scope
How to authorize using graphql-ruby
How to implement a circular profile image in Rails using CarrierWave and R Magick
[Rails API + Vue] Upload and display images using Active Storage
How to input multiple images at once using rake task
Prevent operations! How to securely update Rails manually using transactions
How to set and describe environment variables using Rails zsh
How to deploy jQuery in your Rails app using Webpacker
[Rails] How to use gem "devise"
Image upload using CarrierWave ~ Rspec test ~
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[rails] How to display db information
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
How to use Ruby on Rails
How to deploy Bootstrap on Rails