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.
Gemfile
gem 'carrierwave'
gem 'mini_magick'
Terminal
% bundle install
Terminal
rails g uploader image
After execution, image_uploader.rb will be created under app / uploaders.
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
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