/ File Upload / Image Uploading with Paperclip Gem

Paperclip Gem: https://github.com/thoughtbot/paperclip

1. Install ImageMagic

If you’re on Mac OS X, you’ll want to run the following with Homebrew:

brew install imagemagick

 

2. Include it on the gemfile

gem 'paperclip', '~> 4.3', '>= 4.3.6'

 

3. Run paperclip migration generator

rails generate paperclip user avatar

 

rake db:migrate

 

4. Update User model

class User < ActiveRecord::Base
  has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end

 

4. Update View

<%= form_for @user, url: users_path, html: { multipart: true } do |form| %>
  <%= form.file_field :avatar %>
<% end %>

 

 

No video for this post.

by ryzalyusoff