Skip to content

Instantly share code, notes, and snippets.

@mathildathompson
Last active August 29, 2015 13:57
Show Gist options
  • Save mathildathompson/9719582 to your computer and use it in GitHub Desktop.
Save mathildathompson/9719582 to your computer and use it in GitHub Desktop.
#Make sure the postgres App is running on computer
#Carrierwave works with ORM such as Active Record and Datamapper;
rails new carrier_wave_app -d postgresql
add production and development database to database.yml #make sure you do not add one of the fields to production
rails g scaffold art_galleries name:string location:string avatar:text #Also create the table in migrations folder;
#Is it added to the protected attributes method in the controller;
#Add root_to in the config.routes;
gem 'carrierwave'
rails generate uploader Avatar #Give you a class in app/uploaders/avatar_uploader.rb
rails g migration add_avatar_to_art_galleries avatar:string
rake db:create
rake db:migrate
mount_uploader :avatar, AvatarUploader #Add this to the ArtGallery model allows us to use all the methods from AvatarUploader class;
<%= form_for @painting, :html => {:multipart => true} do |f| %> #Add multipart true to the form so it can handle file attachments;
<%= f.label :avatar %><br>
<%= f.file_field :avatar, :accept => "image/gif, image/jpeg, image/svg, image/jpg, image/png", required: :required %>
<%= f.label :remote_avatar_url, "or image URL" %><br />
<%= f.text_field :remote_avatar_url %> #Need to add this to attr_accessible list
#The images are stored in :public/uploads/art_gallery/avatar....
u.avatar.url # => '/url/to/file.png'
u.avatar.current_path # => 'path/to/file.png'
u.avatar.identifier # => 'file.png
#Change the art_gallery params in the nested attribtues bit;
<%=image_tag @art_gallery.avatar %> #Change to an image_tag in the views
##IMAGE RESIZING
#You must have Imagemagick and MiniMagick installed for image resizing;
gem 'mini_magick'
brew install imagemagick
include CarrierWave::MiniMagick #module that is going to add methods to our uploader
process :resize_to_fit => [800, 800]
version :thumb do #creates a new version, which we pass a name
process :resize_to_fill => [200,200] #Going to resize the image to fit inside the specified dimensions;
end
uploader.url # => '/url/to/my_file.png' # size: 800x600
uploader.thumb.url # => '/url/to/thumb_my_file.png' # size: 200x200
##PUSH TO HEROKU TO SHOW CLASS WILL NOT HOST IMAGES
*** REMOVE ALL MINI MAGICK STUFF!!
gem 'rails_12factor'
Make sure production database;
heroku login
heroku create --addons heroku-postgresql
heroku run rake db:migrate
##AMAZON SW3
#You cant store image uploads on heroku; Have to use 3rd party hosting service;
#Interact with a variety of file services #carrierwave handles the fog interaction;
gem "fog", "~> 1.3.1"
#change the store to fog as opposed to storage file in the avatarUploader class;
storage :fog
#Sign into Amazon AWS Web hosting account & go to s3 Web Hosting service;
#Create a new bucket with region Sydney;
#Go to security credential which is a drop down from your name;
gem 'dotenv-rails', :group => development
.env #make sure this never gets committed to Github add it to the Gitignore #Add it to the .gitignore /.env
AWS_SECRET_KEY =
AWS_KEY_ID =
.gitignore .env
carrierwave.rb #Go to app/config/initializers/carrierwave.rb
/config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => ENV['AWS_KEY_ID'], # required
:aws_secret_access_key => ENV['AWS_SECRET_KEY']
}
config.fog_directory = 'carrierwavesw3' # required #The name of your bucket on Amazon SW3;
config.fog_public = false # optional, defaults to true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end
#DELETE ALL OF THE MINI MAGICK STUFF;
#File needs to be in a separate form that is different from the name of the image;
heroku config:add AWS_SECRET_KEY =
heroku config:add AWS_KEY_ID =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment