Introduction to Ruby on Rails
Course Length: 5 days
Delivery Methods:
Available as private class only
Course Overview
In this Ruby on Rails training course, students will learn to create database-driven websites using Ruby on Rails. They will learn to install Ruby and Rails, to program in Ruby and use Ajax in their Ruby on Rails applications. This course can be customized to your team's learning needs.
Course Benefits
- Learn to install and configure Ruby and Rails
- Understand the Model-View-Controller framework architecture
- Learn to program in Ruby
- Learn to build database-driven applications with Ruby on Rails
- Learn to work with templates
- Learn to use Ajax in Ruby on Rails applications
- Learn to test Ruby on Rails applications
Course Outline
- Rails, Breadth-First
- Welcome to Ruby on Rails
- Divide and conquer: the Model/View/Controller (MVC) architecture
- The Ruby story
- What Rails Does (and Does for You)
- Inside MVC in Rails
- How Separate Are the MVC Layers?
- A Guided Tour of a Small (but Working!) Rails App
- The Rails Application Directory Structure
- app/
- test/
- public/
- config/
- script/
- db/
- Rails environments
- A word about Rake
- Connecting to a Rails Application
- Try it out!
- The Rails Request/Response Cycle
- An action, up close
- The view
- Some Pre-Auction Housekeeping
- Creating the directory for the application
- Setting up a default route
- Adding a default layout
- Tweaking the gem set
- Welcome to Ruby on Rails
- Ruby Skills and Tools
- Running Ruby Programs
- IRB (Interactive Ruby)
- Try it out!
- Objects and Methods
- Sending messages to objects
- Messages and methods
- Catching non-existent methods
- Teaching old objects new tricks
- The "self" keyword
- Classes
- Inheritance
- Classes as objects
- Strings and Numbers
- String literals and interpolation
- String operations
- String#upcase, #downcase, and #capitalize
- String#strip, #lstrip, and #rstrip; #chop and #chomp
- String#size (or #length)
- Numerics
- Arithmetic operators
- Arithmetic operators methods!
- Symbols
- Arrays
- Arrays as method return values
- Array operations
- Getting items from an array
- Square brackets are (you guessed it!) a method
- Setting array items
- Hashes
- Hash operations
- Determining hash key uniqueness
- Alternate hash key syntax
- Modules
- Modules as namespaces
- Modules as class mixins
- Control Flow and Conditionals
- Instance Variables and Attributes
- Instance variables in Rails actions
- Attributes
- Revising an earlier example, with attr_accessor
- Setting and reading a car's fuel
- Read-only attributes (and the secret of how attributes work!)
- Why you (should) care about attributes
- Exercise: explicit vs automatic attribute methods
- Iterators
- A first iterator example
- Yield to the block!
- Passing values to a code block
- A first iterator example
- Sorting Collections
- Sorting with a code block
- sort_by
- Filtering Collections
- find and find_all (synonyms: detect and select)
- map (synonym: collect)
- Exercises
- The "What": Domain Modeling and ActiveRecord
- ActiveRecord Overview
- Domain Modeling, Ruby, and Rails
- The Scratchpad Domain
- Exercise: Modeling the Auction Domain
- Programming the Domain
- Examining a New Model File
- Declaring ActiveRecord Associations
- Exercise: Specifying the Associations
- Creating the Database Tables
- Customizing the Boilerplate Migrations
- Association Foreign Keys in Migrations
- Exercise: Initializing the Database with Migrations
- Alternative Migration Syntax
- The Rails Application Console
- Starting the Console
- Entering Console Commands
- Creating Objects in the Console
- Exercise: Creating a User, Item, and Auction
- Routing and Responses
- The Rails Request/Response Cycle
- URL Recognition and Routing
- Named Routes and URL Generation
- Named Route Arguments
- Matching Routes the "longhand" Way
- Resource Routing
- Using Named Routes for Resources
- Creating Resource Routing for the Auction
- Nested Resources
- Do You Really Need to Keep Saying id, id ?
- Examining All Your Routes
- A Word on REST
- Using Named Routes in the Application Console
- Creating Controllers and Views
- Auto-generating View Template Files
- Exercise
- Answer
- How Rails Chooses a Template
- Understanding ERB Templates
- Using Embedded Ruby (ERb)
- Trying ERb Syntax
- Exercise: Fleshing Out the Auctions/Index View
- Exercise: Adding an Auction/Show Action
- A Couple of Tweaks to the Show View
- Exercise: Add a Help Page
- Forms
- Displaying and Processing Forms in Rails
- The Basics of Writing a Rails Form
- Update Forms and Pre-filled Form Fields
- Form Submission and Processing
- The Payoff
- New/Save, Create, and update_attributes
- Strong Parameters
- A Few Other form Helper Methods
- password_field
- text_area
- select
- check_box
- label
- Date selection
- The *_tag (non-magic) helpers
- Redirecting to a Different Action
- Exercise: Write an Item-editing Form and an Update Action
- The Actions
- The Views
- A Little More about the Flash Facility
- flashnow
- Creating a New Auction
- Exercise: Write the newhtmlerb (creation form) View for Auctions
- The Auctions/Create Action
- Footnote: accepts_nested_attributes_for
- Displaying and Processing Forms in Rails
- Exploring ActiveRecord Techniques
- Getting into ActiveRecord
- Refining AR Associations
- An Auction Belongs to a Seller
- Migrating user_id to seller_id
- Changing belongs_to :user to belongs_to :seller
- Changing has_many :auctions to reflect "seller"
- Exercise: Bids and Bidders
- Many-to-Many Associations with "Through"
- The :source parameter
- Exercise: has_many :through the other way around
- The Result: Many-to-Many Associations
- "Old-style": has_and_belongs_to_many
- has_and_belongs_to_many vs has_many :through
- Conditions and Limits in Associations
- Exercise: Write an Association for Most Recent Bids
- Miscellaneous Custom Methods in the Models
- Exercise: Writing Custom Methods
- Submitting a Bid
- ActiveRelation Basics
- Enhancing the Views Based on Model Logic
- Next Stop: Testing
- Testing
- Ruby's Test Framework
- MiniTest Basics
- A First Set of Tests
- The Alternative Test Syntax
- Testing Classes and Objects
- Making Changes to Tested Code
- Exercise: Fixing a Test Failure
- Answer
- From Minitest to Rails Testing
- Initializing the Test Database
- Fixtures and How to Write Them
- Where Fixtures Live
- Preprocessing of Fixture Files
- Associations in Fixture Files
- Exercise: Writing User, Item, and Bid Fixtures
- Answer
- Examining Fixtures in the Application Console
- Using Fixtures in Unit Tests
- Some First Unit Tests
- Running the Tests
- Exercise: Testing the Custom Methods
- Answer
- Functional Tests
- The default functional test file
- A first functional test: testing title assignment
- Running the functional tests
- Examining the content of response HTML
- Exercise: Write some functional tests
- Answer
- The Rails testing landscape
- Ruby's Test Framework
- Data Validation with ActiveRecord
- Validating ActiveRecord Objects
- How (and Where) the Validation Methods Are Called
- Application-level vs Database-level Data Validation
- How an Object's Validity Is Determined
- Saving, Updating, and Creating Invalid Objects
- The Role of the Controller in Validating Data
- Why set @item?
- Displaying the Error Messages
- Examples of Selected Validation Methods
- length/size
- format
- uniqueness
- Exercise: Adding Validations to the Models
- Answers
- Require that Starting Bids Consist onLy of Integers
- Modifying the Items/Update Action
- Validating ActiveRecord Objects
- Exploring ActionPack
- ActionPack, the composite library
- Custom Helper Methods
- When to Write Helper Methods
- Partial Templates
- Refactoring Forms with Partials
- Moving the Duplicate Code into the Partial Template
- Exercise: Reuse the Form Partial
- Answer
- Rendering Collections in Partial Templates
- The Session Hash
- Controller Filters
- Writing a Before Filter
- Halting Execution in a Before Filter
- Code Blocks as Before Filters
- The Scope and Ordering of Filters
- After and Around Filters
- Differential Responses
- Using Alternative Templates
- XML with Builder
- Next Stop: Cumulative Exercise
- Cumulative Exercise: Users and Bids
- Consolidating Your Gains
- Exercise Description 1: High-level
- Exercise Description 2: Thick Description
- Passwords
- User Registration: Name, Email, Password
- Logging In and Out
- Sessions and the @current_user
- Fleshing Out Seller and Owner
- Bidding
- Securing the Item Editing Process
- Showing Links
- Tests, Old and New
- The Results
- The login/logout Routes
- User Authentication with the Session Hash
- Requiring a User at the Filter Level
- Fleshing Out Seller and Owner
- Creating a Bid
- Securing the Item Editing Process
- Selected Tests
- Using CSS and JavaScript
- The Basic Asset Pipeline Architecture
- Concatenation and Minification of Assets
- An Example: Toggling Display of the Scratchpad Comment
- Exercise: Displaying and Hiding Bid History
- Ajax
- Example: Ajax in the Scratchpads Application
- Exercise: Make Bidding an Ajax Request
- The End of the Line!
- The Basic Asset Pipeline Architecture
Class Materials
Each student will receive a comprehensive set of materials, including course notes and all the class examples.
Class Prerequisites
Experience in the following is required for this Ruby on Rails class:
- HTML
Experience in the following would be useful for this Ruby on Rails class:
- Server-side programming experience
- SQL