Skip to content

Instantly share code, notes, and snippets.

@wearethefoos
Created October 31, 2015 10:00
Show Gist options
  • Select an option

  • Save wearethefoos/294cc3e849cb2136a350 to your computer and use it in GitHub Desktop.

Select an option

Save wearethefoos/294cc3e849cb2136a350 to your computer and use it in GitHub Desktop.

Carrierwave Example Uploader

Steps

  • Add gem 'carrierwave' to Gemfile
  • Run bundle install to 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 image by 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_field to f.file_field.
  • Change all your image_tags to not use model.image, but model.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? %>
  • Add public/uploads to your .gitignore file.

TIP Check out my changes in this commit!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment