Ruby on Rails: Introduction to Ruby on Rails April 8th, 2015

  • rails -v to check version
  • Rails takes MVC further: when you develop in Rails, you start with a working application, there’s a place for each piece of code, and all the pieces of your application interact in a standard way
  • Once you create an application with a specific version of Rails, it will continue to use that version of Rails, even if newer versions are installed on the system, until you decide it is time to upgrade (stored in the Gemfile at the project root)
  • All Rails applications have testing support baked right in
  • As you add functionality to the code, Rails automatically creates test stubs for that functionality
  • Rails programs are shorter and more readable due to DRY and convention over cofiguration
  • Support for Ajax and RESTful interfaces is built in
  • Deploy or roll-back with a single command
  • Rails documentation: If you install Rails using RubyGems, simply start the gem documentation server (gem server), and you can access all the Rails APIs by pointing your browser at http://localhost:8808
  • Rails is “full stack” = “database to view”
  • Encourages convention over configuration
  • Rails released July 2004 (due to the popularity of Ruby) – created by David Heinemeier
  • rails new <APP-NAME> (app [models, views, controllers, assets, etc.], bin [scripts e.g. rails, rake, bundle], config, db, lib, log, public, test, tmp, vendor)
    • The rails new command will take a little while to run since it generates the beginning of your Rails app and downloads all the basic gems needed to run a Rails app
    • Rails assumes you will use Git, adding the .gitignore and .keep
    • Rails creates a specific directory structure to be able to find everything programmatically (the magic of Rails … e.g. by default, Rails looks for templates in a file with the same name as the action it’s handling)
    • Use rake about to examine your installation
  • Rails uses SQLite 3 by default
    • Generally speaking, any site that gets fewer than 100K hits/day should work fine with SQLite. The 100K hits/day figure is a conservative estimate, not a hard upper bound. SQLite has been demonstrated to work with 10 times that amount of traffic.
  • bin/rails server (makes server run faster) or rails server to start Rails server (Ctrl+c to stop)
  • bin/rails console to start irb in Terminal within your app
  • Spring is an application pre-loader (speeds dev tasks)
  • Bundler is itself a gem
  • bundle install ensures all required gems are installed
  • bundle show
  • bundle check
  • bundle show <gem>
  • bundle update <gem>
  • localhost:3000
  • No need to recompile! (except config for example)
  • MVC architecture design pattern
    • User uses controller … manipulates model … updates view … which user sees
  • Rails is a request/response framework
    • Requests are GET, POST, PUT, DELETE
    • Responses are HTTP responses, have a status code (e.g. 200 OK, 302 Found, 404 Not Found)
  • <%= %> … executed AND output
  • <% %> … executed
  • Rake is the build system for Ruby (you use rake to create the database)
  • Controller actions render views
  • If no explicit render is called, Rails automatically renders the template with the same name as the action
  • Multiple extensions … index.html.erb … .html tells Rails the template generates HTML … .erb tells Rails to use the ERB (Embedded Ruby) templating system
  • reload! in the console to reload classes
  • We are using the helper methods number_with_delimiter and number_to_currency to format the page count and price
    • Rails has many built in helper methods
    • See the API docs or Rails Guides online
  • (e.g. books_path) is a generated function that comes from the name of the route in routes.rb
  • You can see all the routes using bin/rake routes or by navigating to http://localhost:3000/rails/info/routes
  • Class example … class Mine < Master … the Mine Class inherits from the Master class
  • Typical file name … hello.html.erb … ERB is a filter that is installed as part of the Rails installation that takes an .erb file and outputs a transformed version
    • Content between <%= and %> is interpreted as Ruby code and executed
  • In development mode you don’t need to restart the server when you make changes
  • Rails uses a convention to parse the URL into a target controller and an action within that controller
    • <%= link_to(“DISPLAY_TEXT”), CONTROLLER_ACTION_path %>
  • In views use <% … %> to inject Ruby and <%= … %> to inject Ruby value substitution
  • MVC … Model View Controller … Request comes into router which picks the appropriate controller, controller talks to model, model talks to database, model returns to controller, controller talks to view, view is rendered to the browser
    • Active Record is the ORM model layer supplied with Rails
      • ORM maps tables to classes, rows to objects, and columns to attributes
    • Action Pack is the view and controller layers in Rails
      • The controller is also home to a number of important ancillary services…
        • It is responsible for routing external requests to internal actions; handling people-friendly URLs extremely well
        • It manages caching, which can give applications orders-of-magnitude performance boosts
        • It manages helper modules, which extend the capabilities of the view templates without bulking up their code
        • It manages sessions, giving users the impression of ongoing interaction with our applications
  • A “magic” command worth knowing is rake db:migrate:redo which will undo and reapply the last migration
  • You can redo DB migrations with the following sequence…
    $ rake db:drop
    $ rake db:create
    $ rake db:migrate
    $ rake db:seed

Basic Command Flow

  • rails new <app_name> to create the new project
  • rails server to start the local server
  • rails generate controller NAME [action action] [options] to create a new controller (NAME should be initial-capped and then camel-case since it becomes a Ruby class)
    • Modifies routes.rb, creates JS and CSS assets, creates controller, creates helper, creates views, and creates test controller

Resources