-
Notifications
You must be signed in to change notification settings - Fork 0
Paperclip
Tim edited this page Jul 1, 2015
·
2 revisions
Adding Images to site with Paperclip
- `$ brew install imagemagick' - Install ImageMagick its a code library that deals with image compression.
- Add
gem 'paperclip' to gemfile, runbundle` - 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-
$ rails generate paperclip <modelname> image- migration to add this image to our model, then runrake db:migrateto add the new columns. - 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 %>` - To view the saved image:
(index - view thumbnail)
<%= image_tag restaurant.image.url(:thumb) %> - To the controller add
:imageto your permits statement.
Instructions taken from yelpv3