- Add
gem 'carrierwave'to Gemfile - Run
bundle installto install the new gem - Stop spring:
spring stop - Run
rails generate uploader Image - Mount the newly generated ImageUploader on your Model. I mounted it on
the string type column
imageby adding this in my model's class:
class Product
mount_uploader :image, ImageUploader
end- In your Form view, add the option
type: :file, e.g.:form_for(@post, type: :file)
- Again in your Form view, change the type of your image field from
f.text_fieldtof.file_field. - Change all your
image_tags to not usemodel.image, butmodel.image.url- NB: make sure to check if the image exists, or things will break!
Example:
<%= image_tag luxury_coffee.image.url if luxury_coffee.image.present? %>
- NB: make sure to check if the image exists, or things will break!
Example:
- Add
public/uploadsto your.gitignorefile.
TIP Check out my changes in this commit!