Skip to content
Tim edited this page Jul 1, 2015 · 2 revisions

Adding Images to site with Paperclip

  1. `$ brew install imagemagick' - Install ImageMagick its a code library that deals with image compression.
  2. Add gem 'paperclip' to gemfile, run bundle`
  3. Add code to model
class <modelname> < ActiveRecord::Base
  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
  1. $ rails generate paperclip <modelname> image - migration to add this image to our model, then run rake db:migrate to add the new columns.
  2. To save the file in the view (new - top of the form) %= form_for @restaurant, html: {multipart: true} do |f| %> (new - in the form, to add the file select) <%= f.file_field :image %>`
  3. To view the saved image: (index - view thumbnail) <%= image_tag restaurant.image.url(:thumb) %>
  4. To the controller add :image to your permits statement.

Instructions taken from yelpv3

Clone this wiki locally