- Startup Rails App
- Gems
- RSpec
- Factory Girl
- User Authentication
- Show All Routes
- Make a Model
- Make a Controller
- Add & Remove Columns from Migrations
- Resource Routing
- Callbacks & Filters
- Heroku Deployment
Do this after forking from master, make sure you're on your own branch.
- cd into your 'project-forks' folder
echo 'ProjectName' > PROJECTFOLDER/.ruby-gemset
- ex)
echo 'TaskListRails' > C3Projects--TaskListRails/.ruby-gemset
- ex)
echo '2.2.2' > PROJECTFOLDER/.ruby-version
- ex)
echo '2.2.2' > C3Projects--TaskListRails/.ruby-version
- ex)
- cd in to project folder (wrappers happen...)
rvm gemset list
(yes it's on the gemset we just created)gem install bundler
gem install rails --no-ri --no-rdoc
git add .
git commit -m "Created gemset."
rails new . -T
git add .
git commit -m "fresh Rails install"
- open and edit
Gemfile
with gems, save! bundle install --without production
- better errors (in
group :development
):
# Better Errors for debugging
gem 'better_errors'
gem 'binding_of_caller'
- Pry Rails (in
group :development
):gem 'pry-rails'
- RSpec (in test section):
gem 'rspec-rails'
rails generate rspec:install
- Add
--format doc
to .rspec file to see words
- Factory Girl (in
group :test
):gem 'factory_girl_rails', '~> 4.0'
- Add to config block in spec_helper.rb
config.include FactoryGirl::Syntax::Methods
andrequire 'factory_girl'
to top of file - Create file to define factories:
touch spec/factories.rb
- Add to config block in spec_helper.rb
- SimpleCov (in
group :test
):gem 'simplecov', require: false
- add to spec/spec_helper.rb
require 'simplecov' SimpleCov.start 'rails'
- add
coverage
to .gitignore
- Bootstrap:
gem 'bootstrap-sass'
- create custom.css.scss file for your own custom CSS
- add to custom.css.scss
@import "bootstrap-sprockets"; @import "bootstrap";
- add to app/assets/javascripts/application.js
//= require jquery //= require bootstrap-sprockets
- restart rails server if already open
- bcrypt (click link for more info):
gem 'bcrypt'
- add
has_secure_password
to User model associations
- add
- CarrierWave: add
gem 'mini_magick'
BEFOREgem 'carrierwave'
- If imagemagick is not installed:
brew install imagemagick
rails g uploader image
, image can be called anything: avatar, cover_art, etc.rails g migration adds_image_to_albums
, to add a reference column (for the image) to database- Add
add_column :albums, :image, :string
to new migration file. rake db:migrate
- Open Album Model and add
# Mounted Objects -----------------------
- Add
mount_uploader :image, ImageUploader
under "Mounted Objects" - Go to uploaders/image_uploader.rb, uncomment line 7
include CarrierWave::MiniMagick
- line 35 block allows you to resize image as it's uploaded, uncomment to resize images to thumbnails (50px by 50px)
- add another block to create another version of image transformation (if desired)
- uncomment file extensions on line 41 to allow only certain file ext. (jpg, png, gif)
- Add
:image
to albums_params in AlbumsController - Able to use these methods
.image_url
and.image_url(:thumb)
- Add
public/uploads/
to .gitignore
- If imagemagick is not installed:
- HTTParty:
gem 'httparty'
require 'HTTParty'
wherever you're using it- Example:
weather_url = "http://api.openweathermap.org/data/2.5/weather?q=Seattle&units=imperial"
r = HTTParty.get(weather_url)
- OmniAuth
- Note different providers have their own gems they maintain and one can use:
# OmniAuth Gems gem 'omniauth' gem 'omniauth-github'
- Go to GitHub to register new application (in profile settings --> applications --> Developer Applications -- Register New Application)
- Authorization callback URL:
http://localhost:3000/auth/github/callback
- Follow remaining steps here: OmniAuth
- VCR (in
group :test
):gem 'vcr'
- VCR github
- Used to test API calls in RSpec
- if deploying to heroku: move sqlite3 gem to development, then add
group :production do
gem 'pg'
end
rake routes
rails generate model modelname columnname1:type columnname2:type columname3:type
- Example:
rails generate model student name:string cohort:string birthday:datetime
rails generate controller controller_name
- Example:
rails generate controller tasks
- convention says controller name is plural
- i.e. ClientsController preferred over ClientController
rails generate migration add_columnname_to_tablename column:type
- Example:
rails generate migration add_personid_to_tasks personid:integer
rails generate migration remove_columnname_from_tablename column:type
- Example:
rails generate migration remove_personalid_from_tasks personid:integer
resources :labels
resources :labels, only: [:index, :show]
resources :labels, except: [:index, :show]
resources :labels do
# 8 more routes generated
resources :albums
end
Shouldn't nest more than one deep
resources :labels do
resources :albums
resources :artists do
resources :albums, only: [:index, :show]
end
end
resources :albums, only: [:index, :show] do
collection do
get 'released/:year', action: 'by_year', as: 'by_year'
end
end
get 'released/:year' => 'albums#by_year'
as: 'bye_year'
allows forby_year_albums_path
for linking- also have
by_year_albums_url
for linking offsite
- Add gem to development section
gem 'rspec-rails', '~> 3.0'
bundle
rails generate rspec:install
- Add
--format doc
to .rspec file to see words
- To generate tests for Album model
rails generate rspec:model Album
- Go to spec -> model -> album_spec to edit specs
- Edit specs
- To run, use
rspec
command in terminal
- To generate tests for Albums controller
rails generate rspec:controller albums
- Go to spec -> controllers -> album_controller_spec to edit specs
- Edit specs
- To run, use
rspec
command in terminal
To run a spec for a specific folder
rspec folder/path
For example, rspec spec/controllers
Make sure your sqlite3 gem is under development group and add gem 'pg' is in production group
- Make sure current project is committed,
git commit -m "Your message here"
heroku create NAMEOFSITE
- ex:
heroku create myapp
will create myapp.herokuapp.com
- ex:
git push heroku branch:master
heroku run rake db:migrate
heroku run rake db:seed
heroku pg:reset DATABASE
heroku run rake db:migrate
heroku run rake db:seed
heroku restart
- add
gem 'bcrypt'
bundle
rails g model User name:string email:string password_digest:string
- Add
has_secure_password
to User model (-> app/model/user.rb)- Methods available for models w/ 'password_digest defined':
user.password
anduser.password_confirmation
- will not save password if these two values don't match
user.authenticate
allows one to check if password matches existing storedpassword_digest
User.find_by(name: 'david').try(:authenticate, 'newd00les')
- Methods available for models w/ 'password_digest defined':
- Add
home_controller.rb
androot 'home#index'
to show a home page rails g controller sessions
- Add
new
,create
, anddestroy
methods to SessionsController.
root 'home#index'
resources :users
resources :sessions, :only => [:new, :create, :destroy]
Callbacks allow you to trigger logic before or after an alteration of an object's state.
- Example:
before_validation :ensure_login_has_a_value
- Example:
after_validation :set_location, on: [ :create, :update ]
Filters are methods that are run before, after or "around" a controller action.
- Example:
before_action :require_login
- Example:
skip_before_action :require_login, only: [:new, :create]
- Example:
around_action :wrap_in_transaction, only: :show
- Defining a factory within Book model:
FactoryGirl.define do
factory :book do
name "House of Leaves"
author "Mark Z. Danielewski"
description "House of Leaves is the debut novel by the American author Mark Z. Danielewski, published by Pantheon Books. The novel quickly became a bestseller following its release on March 7, 2000. It was followed by a companion piece, The Whalestoe Letters"
end
end
- Using in testing:
describe Book do
describe "validations" do
it "is valid" do
expect(create(:book)).to be_valid
end
it "is invalid without a name" do
expect(build(:book, name: nil)).to be_invalid
end
end
end
- old way:
@book = Book.create(name: "", author: "", description: "")
- factory way:
@book = create(:book)
10.times { create(:book) }
@unsaved_book = build(:book)