Skip to content

Instantly share code, notes, and snippets.

@Tasha25
Created November 13, 2013 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tasha25/7454073 to your computer and use it in GitHub Desktop.
Save Tasha25/7454073 to your computer and use it in GitHub Desktop.
Paperclip for images draft.
rails new app
rake db:create; rake db:migrate
Added new input fied so you can add photos.
If you have folder you can just click on the button and we will upload it
brew install imagemagick
//when you upload file this will handle the different size rendering
We then have to use a gem paperclip
//go into directory
$sublime .
//go into your gem file
$gem 'paperclip'
run bundle
To all a model to use a paperclip
rails generate papeclip "nameof model" "name of attachment"
rails generate paperclip User image
it creates a migration file and upload the tables
if you look into your table you will see a new migraction
run
rake db:migrate
Now you have to worry about the form
You will go to user form and add to the bottom of the form for
<div>
<%= f.file_field :image%>
</div>
//creates an input field with a type of file
Form_for will give you specific names and title for you field
After you try to upload you will get a mass assignment area.
So you will have to add image to user.rb
class User <ActiveRecord::Base
attr_accessible :name, :image
has_attached_file :image,
:styles => { :medium => "300x300>",
:thumb => "100x100>" },
:default_url => "/images/:style/missing.png"
end
Nes you have to go to show.htmleerb to show image
<%= @user.name %>
<%= image_tag(@user.image.url(:thumb)) %> //using rails helper image_tag
</p>
//the image is placed into the public folder and system paperclip as placed our image.
//allows paperclip to know how to handle this.
If you want to push to heroku you have to do a different thing
You got to a folder that doesn't have a git repo
STEPS FOR HEROKU
git init
heroku create
//you have to do change in the application.rb
config.assets.initialize_on_precompile = false
git add .
git commit -m 'update'
git push heroku master
When you push things to heorku you have to keep in mind that images do not save so you have to use a third
party server such as amazon web service.
heroku run db:migrate
heroku open
//it will show image
heroku reset
// this will
We are interesed in Amazon Simple Storage Service and we will be interested in S3
we are interested in buckets
each bucket have their own file storage system
Go into gemfile
gem 'aws-sdk'
go into terminal
$bundle
When you get setup with s3 you have to create a unique bucket.
you pick US standard region and press create
you need to know bucket name and a key
you go to account activity to
you goto account and security credentials to see what your keys are
they will also give you your access token
Inside of development.rb if you want to run locally you need to put the info
##this can go in a gist title Paperclip
## place this in development.rb to run locally
## place this in production.rb to run heroku
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
Inside of production.rb if you want to run locally you need to put the info
$ sublime ./bashrc
in bash profile you can put
#--------------
##AWS KEYS
#--------------
export AWSKEY='kry'
export AWS_SECRET-KEY=' '
export ...
you then have to reload you terminal
$reload
You then need to fix your initializers
found in config
mkfile paperclip.rb
And inseert the following:
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
the setting for this is usually serialize. We will want to change the form to undersatnd that we will have images
This will be in index.html.erb
<%form_for(@user, html: {multipart: true}) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%=f.file_field :image %>
<%= f.submit %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment