[RUBY] [Rails] Introduce Carrierwave

For a blog app that allows you to post only the title and text Introducing Carrierwave when the photo posting function is added I summarized the procedure.

1. Edit the Gemfile file

Gemfile


gem 'carrierwave'
gem 'mini_magick'

Terminal


% bundle install

2. Create an uploader

Terminal


rails g uploader image

After execution, image_uploader.rb will be created under app / uploaders.

3. Mount the uploader

app/models/message.rb


class Message < ApplicationRecord
  belongs_to :group
  belongs_to :user
  belongs_to :heven

  validates :content, presence: true, unless: :image?

  mount_uploader :image,ImageUploader ⬅️ This one line
end

4. Allows image resizing

Uncomment ** include CarrierWave :: MiniMagick ** Add ** process resize_to_fit: [800, 800] ** to any line. This allows you to resize the aspect within 800px while maintaining the aspect ratio.

app/uploaders/image_uploader.rb


class ImageUploader < CarrierWave::Uploader::Base
  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick ⬅️ Activate

#~abridgement~

  process resize_to_fit: [600, 600]⬅️ Addendum

#~abridgement~
end

Now you are ready to upload the image.

Thank you for visiting.

Recommended Posts

[Rails] Introduce Carrierwave
Introduce Rails Presenter
Introduce Vue.js to Rails
[Rails] Save images using carrierwave
Until you introduce fonts to Rails
[Rails] Voice posting function ~ Cloudinary, CarrierWave
How to introduce jQuery in Rails 6
[Rails g.error]
Rails basics
Rails Review 1
Rails API
Rails migration
I tried to introduce CircleCI 2.0 to Rails app
[Rails] first_or_initialize
[Ruby on Rails] How to use CarrierWave
rails tutorial
[Rails] How to upload images using Carrierwave
Introduce two-factor authentication to your Rails application
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
[Rails] Image posting by CarrierWave [AWS EC2]
Preparing to introduce jQuery to Ruby on Rails
[Rails] devise
[Rails 5.x] How to introduce free fonts
rails tutorial
rails tutorial
Rails Tips
rails method
rails tutorial
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
Multiple image upload function using Rails Carrierwave
Upload multiple images easily with rails rails + carrierwave + cloudinary
[Rails] How to upload multiple images using Carrierwave
Introduce dotenv to Docker + Rails to manage environment variables
[Rails] How to easily introduce slick (slider function)