Skip to content

Instantly share code, notes, and snippets.

@mrgcohen
Created February 7, 2015 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrgcohen/bbde44d4ca7f6f570799 to your computer and use it in GitHub Desktop.
Save mrgcohen/bbde44d4ca7f6f570799 to your computer and use it in GitHub Desktop.
Rails Intro and Scaffolding

Prototyping in Rails

I'm assuming you're on Linux or a Mac. If you aren't - Google is your friend. Fortunately there's a ton of documentation out there so you shouldn't have too much trouble setting things up.

Machine Setup

RVM and Ruby-2.2.0 Installation

  1. Install RVM to manage Ruby - Go to https://rvm.io/ and install (should be 1-2 calls to install)
  2. Lets install ruby-2.2.0 and make it our default
# install rvm (taken from https://rvm.io/)
gpg --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable

# setup ruby
rvm list
rvm install ruby-2.2.0
rvm use ruby-2.2.0 --default

# now check
which ruby

Install Rails Gem and Bundler

  1. Install rails gem and bundler
  2. Rails contains a bunch of documentation so we are going to skip that to make the install fast with --no-ri --no-rdoc
gem install rails --no-ri --no-rdoc
gem install bundler --no-ri --no-rdoc

Lets create our app

Exercise App Creation

I'm a cycling addict and like everyone else I'm obsessed with numbers. Lets create an app to track all our activity

  1. Lets initialize a new rails app
  2. Then start generating my exercise bulimia Models, Views, and Controllers. Along with a bunch of other associated items that rails makes for us.
# new app
rails new exercise_addict
cd exercise_addict

Additional Gems

  • Add to Gemfile
# ./Gemfile

# Include Bootstrap-sass for prettier generators
gem 'bootstrap-sass', '~> 3.3.1'
gem 'autoprefixer-rails'

# Include Bootstrap Generators
gem 'bootstrap-generators', '~> 3.3.1'

# For Generating Pretty Schemas 
# What we are using to generate our diagrams
# gem 'railroady'
  • Install gems
# For Railroady you may need to install additional requirements (macports, brew, apt-get)
#   sudo port install graphviz
#   brew install graphviz
#   sudo apt-get install graphviz

# Install gems
bundle install 
  • Update JS and CSS manifest files to include Bootstrap

./app/asssets/stylesheets/application.css

@import "bootstrap-sprockets";
@import "bootstrap";

./app/assets/javascript/application.js

//= require bootstrap-sprockets
//= require bootstrap
  • Run to install bootstrap gem for layouts
rails generate bootstrap:install

Generator time

So what do we want to track? I like to start by thinking about how the data-model looks. To start I know I want to track my activites.

Activities

  • Before we generate our activites what do you care about when logging activites?

  • Data Type

rails g scaffold activity exercise:references intensity:references duration:integer distance:integer date:datetime description:text 
  • Lets look at the schema we just made
rake diagram:all
  • Lets take a look at our code.. we should have some a bunch of stuff created for us

    • Controller
    • Model
    • Views html.erb files
    • CSS and JS Starters per controller
    • TestSuite
  • Whoops.. we need to setup our routes to map our default route

# ./config/routes.rb
root controller: :activities, action: "index"

Exercises

rails g scaffold exercise exercise:string description:text

Intensities

rails g scaffold intensity intensity:string description:text

Shoes

rails g scaffold shoe shoe:string brand:string bought:date
  • Lets modify the models to include ORM mappings for the reference tables
# ./app/models/
  • Lets look at the schema
rake diagram:all
  • Generated all CRUD methods and ruby scripts to create tables
  • Time to execute the table creation scripts
rake db:migrate
  • Lets take a look at the code, modify it, and make it use drop-downs etc
  • Modify some of the views
<%= f.collection_select :intensity_id, Intensity.all, :id, :intensity, :class => "form-control" %>
<%= f.collection_select :exercise_id, Exercise.all, :id, :exercise, :class => "form-control" %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment