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.
- Install RVM to manage Ruby - Go to https://rvm.io/ and install (should be 1-2 calls to install)
- 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
- 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
I'm a cycling addict and like everyone else I'm obsessed with numbers. Lets create an app to track all our activity
- Lets initialize a new rails app
- 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
- 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
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
- boolean, date, datetime, decimal, float, integer, string, text, time, timestamp
- check out http://stackoverflow.com/questions/17918117/rails-4-datatypes for all field types available and how each interacts with different databases (postgres, mysql, etc)
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" %>