You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Go to Gemfile and add these lines. Please use the versions indicated as it has some issues on their latest version. gem 'carrierwave', '1.3.1' gem 'cloudinary', '~>1.13.2'
Then, run bundle install
Create a column on the table where you want to add the file. You can edit the migration file if you want to add some attributes. rails g migration add_image_to_post image:string
Run rails db:migrate
Active Storage Installation
Run below command to the terminal to install ActiveStorage rails active_storage:install
Run rails db:migrate
Carrierwave Configurations
Create an uploader for carrierwave. Run below code to terminal: rails g uploader image
A file path app/uploaders/image_uploader.rb is automatically generated after running the code
Go to app/models/post.rb and add below code to mount the uploader in your model mount_uploader :image, ImageUploader Note:ImageUploader is the class used inside image_uploader.rb file
You may add validations in app/models/post.rb if you want to upload a single file (haven't tried to upload multiple files) has_one_attached :image
Cloudinary Configurations
Create an account on Cloudinary https://cloudinary.com/. Once account is created, API keys will be given and it is required when uploading files in the cloud
Store API keys on env variables
Create cloudinary.yml and save it under config directory. Then copy below code.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Initial Configurations
Go to Gemfile and add these lines. Please use the versions indicated as it has some issues on their latest version.
gem 'carrierwave', '1.3.1'
gem 'cloudinary', '~>1.13.2'
Then, run
bundle install
Create a column on the table where you want to add the file. You can edit the migration file if you want to add some attributes.
rails g migration add_image_to_post image:string
Run
rails db:migrate
Active Storage Installation
Run below command to the terminal to install ActiveStorage
rails active_storage:install
Run
rails db:migrate
Carrierwave Configurations
Create an uploader for carrierwave. Run below code to terminal:
rails g uploader image
A file path
app/uploaders/image_uploader.rb
is automatically generated after running the codeGo to
app/models/post.rb
and add below code to mount the uploader in your modelmount_uploader :image, ImageUploader
Note:
ImageUploader
is the class used insideimage_uploader.rb
fileYou may add validations in
app/models/post.rb
if you want to upload a single file (haven't tried to upload multiple files)has_one_attached :image
Cloudinary Configurations
Create an account on Cloudinary https://cloudinary.com/. Once account is created, API keys will be given and it is required when uploading files in the cloud
Store API keys on env variables
Create
cloudinary.yml
and save it underconfig
directory. Then copy below code.config/environments/development.rb
and add below lineconfig/storage.yml
(Auto generated when ActiveStorage is installed) and add below codeapp/uploaders/image_uploader.rb
then include Cloudinary modules/classes to CarrierWave classes. Comment outstorage :file
andstore_dir
methodFront End Side
.url
at the end to get the URL associated with image= image_tag @post.image.url, class: "img img-fluid"
You're now all set! Enjoy!
Beta Was this translation helpful? Give feedback.
All reactions