Skip to content

Instantly share code, notes, and snippets.

@taylortrimble
Last active December 16, 2015 21:29
Show Gist options
  • Save taylortrimble/5500325 to your computer and use it in GitHub Desktop.
Save taylortrimble/5500325 to your computer and use it in GitHub Desktop.

Step by Step

Getting Kicked Off

  1. rails new app-name --skip-test-unit
  2. Better get the Gemfile looking spiffy/installed
  3. Don't forget to get all set up for PostgreSQL
  4. Use RSpec: rails generate rspec:install
  5. Sexify README
  6. Deploy!

Get Stylin'

  1. Add the un-minimized Bootstrap assets to vendor/assets
  2. require bootstrap in application.css.scss and application.js
  3. rails generate controller StaticPages home help --no-test-framework
  4. Make the root route and a dummy view to test Bootstrap
  5. Create a general application layout with HTML5 shim

Spork Me

  1. Install spark gem with Gemfile
  2. Add environment loading to spec/spec_helper.rb
require 'rubygems'
require 'spork'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However, 
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.
  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    # == Mock Framework
    #
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
    #
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr
    config.mock_with :rspec

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = true

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false
  end
end

Spork.each_run do
  # This code will be run each time you run your specs.

end
  1. Configure .rspec to use spark
--colour
--drb
  1. Run spork in the background and enjoy faster RSpec!

How Tos

Adding New Models

  1. Call (for example) rails generate model User name:string email:string replaced with your data
    • Timestamp data is automatically added
  2. rake db:migrate
  3. Set accessible attributes with attr_accessible :name, :email
  4. Consider annotating with bundle exec annotate

Updating Models

  1. rails generate migration add_index_to_users_email
  2. Find the migration in db/migrate
  3. Edit the migration
  4. Run rake db:migrate

Adding Fields to Models

rails generate migration add_password_digest_to_users password_digest:string

  • Parses _to_users
  • Creates new column called password_digest of type string

Adding New Pages

  1. Controller method
  2. Route
  3. View
  4. Optionally: Helpers, style sheets

Named Routes

SampleApp::Application.routes.draw do
  match '/about',   to: 'static_pages#about'
end

Automatically creates the named routes:

about_path => '/about'
about_url  => 'http://localhost:3000/about'

Database

  1. Create a new model or migration
  2. Migrate the database with rake db:migrate

You can fix mistakes with rake db:rollback or rake db:migrate VERSION=0 (replace 0).

Data Validation

Can validate:

  • presence
  • length
  • format
  • uniqueness

For uniqueness, you must enforce at the database level as well due to double-submission and app-database concurrency issues. This is done with a unique database index.

Authentication

  1. Add string password_digest field to user's database model (and migrate)
  2. Add :password and :password_confirmation to user's attr_accessible
  3. Add validations to password
  4. Validate presence of password_confirmation manually
  5. Call has_secure_password in user's model, which performs magic relating password, password_confirmation, and password_digest, and creates an authenticate method

Testing

  1. Create a new test file with rails generate integration_test static_pages
  2. Run tests using the rspec command with optional files/folders

Describe

describe "Home page" do

  it "should have the content 'Sample App'" do
    visit '/static_pages/home'
    page.should have_content('Sample App')
  end
end

Helper Methods

Use vigorously!

Layouts

In a layout:

  • <%= yield(:symbol) returns the result of <% provide(:symbol, 'Symbol info') %>
  • <%= yield %> returns the rest of the view's body

Reference

RESTful Resources

Via resources :users

HTTP Request URI Action Named Route Purpose
GET /users index users_path page to list all users
GET /users/1 show user_path(user) page to show user
GET /users/new new new_user_path page to make a new user (signup)
POST /users create users_path create a new user
GET /users/1/edit edit edit_user_path(user) page to edit user with id 1
PUT /users/1 update user_path(user) update user
DELETE /users/1 destroy user_path(user) delete user

Via resource :library

Rails Console Commands

For data models

  • User.new(name: "Michael Hartl", email: "mhartl@example.com")
  • User.create(name: "A Nother", email: "another@example.org") (Automatically saves)
  • User.find(1)
  • User.find_by_email("mhartl@example.com")
  • User.first
  • User.all
  • foo.valid?
  • foo.reload
  • foo.save
  • foo.destroy

Rails Tools

  • Ruby
  • RVM
  • RubyGems
  • Bundler
  • Rails
  • Git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment