O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Rails: Novice to Ninja, 3rd Edition

Book Description

Rails: Novice to Ninja is an easy-to-follow, practical and fun guide to Ruby on Rails for beginners. It covers all you need to get up and running, from installing Ruby, Rails and SQLite to building and deploying a fully-featured web application. The third edition of this book has been fully updated to cover Rails 5, the latest version of the framework.

Unlike other Rails books, this book doesn't assume that you are an experienced web developer, or that you've used Ruby before. An entire chapter is devoted to learning Ruby in a fun way, using the interactive Ruby console, so you can follow along at home. You'll be an accomplished Ruby programmer in no time! You'll then start using Rails to build a practical, working project: a Reddit-like social news application. As you'll build the app, you'll gain valuable experience of using Rails features such as user authentication, session cookies, and automated testing. The book finishes with chapters on debugging, benchmarking and deployment to a live web server.

Table of Contents

  1. Rails: Novice to Ninja
  2. Notice of Rights
  3. Notice of Liability
  4. Trademark Notice
  5. About Glenn Goodrich
  6. About SitePoint
    1. Preface
  7. Who Should Read This Book
  8. Conventions Used
    1. Code Samples
    2. Tips, Notes, and Warnings
  9. Supplementary Materials
  10. Chapter 1: Introducing Ruby on Rails
    1. History
    2. Development Principles
      1. Optimize for Programmer Happiness
      2. Convention Over Configuration
      3. The Menu is Omakase
      4. No One Paradigm
      5. Exalt Beautiful Code
      6. Value-integrated Systems
      7. Progress Over Stability
      8. Push Up a Big Tent
    3. Building the Example Web Application
      1. Features of the Example Application
    4. Summary
  11. Chapter 2: Getting Started
    1. What does all this cost?
    2. Installing on Windows
      1. Install Ruby
    3. Installing on Mac OS X
      1. Installing Homebrew
    4. Installing on Linux (Ubuntu)
    5. Additional Installation Options
    6. And Now the Fun Begins
      1. One Directory Structure to Rule Them All
      2. Creating the Standard Directory Structure
    7. Starting Our Application
    8. Version Control and Git
      1. Git Basics
    9. Which Text Editor?
      1. Windows and Cross-platform Text Editors
      2. Linux and Mac OS X Editors
      3. IDEs
    10. Summary
  12. Chapter 3: Introducing Ruby
    1. Ruby is a Scripting Language
      1. Compiled Languages
      2. Scripting Languages
      3. The Great Performance Debate
      4. Choose What Works
    2. Ruby is an Object Oriented Language
    3. Reading and Writing Ruby Code
      1. The Interactive Ruby Shell (irb)
    4. Interacting with Ruby Objects
      1. Literal Objects
      2. Variables and Constants
    5. Basic Punctuation in Ruby
      1. Dot Notation
      2. Chaining Statements Together
      3. Use of Parentheses
      4. Method Notation
    6. Object-oriented Programming in Ruby
      1. Classes and Objects
    7. Object-level Functionality
      1. Instance Variables
      2. Instance Methods
      3. Accessor Methods
    8. Class-level Functionality
      1. Inheritance
      2. Modules and Composition
      3. Return Values
    9. Standard Output
    10. Ruby Core Classes
      1. Strings
      2. Numerics
      3. Symbols
      4. Arrays
      5. Hashes
      6. nil Values
    11. Running Ruby Files
    12. Control Structures
      1. Conditionals
      2. Loops
      3. Blocks, Procs, and Lambdas. Oh my!
    13. Summary
  13. Chapter 4: Rails Revealed
    1. Three Environments
    2. Application Dependencies
    3. Bundler
    4. Database Configuration
      1. The Database Configuration File
    5. The Model-View-Controller Architecture
      1. MVC in Theory
      2. MVC the Rails Way
      3. The ActiveRecord Module
      4. The ActionPack Library
      5. ActionController (the Controller)
      6. ActionView (the View)
    6. RESTful-style
      1. In Theory
      2. REST and the Web
      3. REST in Rails
    7. Code Generation
    8. The ActionMailer Component
    9. Testing and Debugging
      1. Testing
      2. Debugging
    10. Summary
  14. Chapter 5: Models, Views, and Controllers
    1. Generating a Model
      1. The Model Generator
    2. Modifying the Schema Using Migrations
      1. Creating a Skeleton Migration File
      2. Creating the stories Table
      3. Running the Migration
    3. Managing Data Using the Rails Console
      1. Creating Records
      2. Retrieving Records
      3. Updating Records
      4. Deleting Records
    4. Generating a Controller
      1. Running the generate Command
      2. Understanding the Output
      3. Starting Our Application … Again
    5. Creating a View
      1. Generating Views with Scaffolding
      2. Creating Static Pages
      3. Creating Dynamic Pages
      4. Passing Data Back and Forth
      5. Pulling in a Model
    6. Summary
  15. Chapter 6: Helpers, Forms, and Layouts
    1. Calling upon Our Trusty Helpers
    2. Enabling Story Submission
      1. Creating a Form
      2. Saving Data to the Database
      3. Redirecting with URL helpers
    3. Creating a Layout
      1. Establishing Structure
      2. Adding Some Style
    4. Enabling User Feedback with the Flash
      1. Adding to the Flash
      2. Retrieving Data from the Flash
      3. Applying Validations
      4. Tweaking the Redirection Logic
      5. Improving the User Experience
    5. Testing the Form
    6. Testing the Model
      1. Analyzing the Skeleton File
      2. Using Assertions
      3. Writing a Unit Test
      4. Running Model Tests
    7. Testing the Controller
      1. Analyzing the Skeleton File
      2. Writing a Controller Test
      3. Running a Controller Test
      4. Writing More Controller Tests
      5. Running the Complete Test Suite
    8. Visiting the Logs
    9. Summary
  16. Chapter 7: Ajax and Turbolinks
    1. Generating a Vote Model
      1. Creating the Model
      2. Examining the Vote Migration
      3. Applying the Migration
    2. Introducing Relationships
      1. Introducing the has_many Clause
      2. Introducing the belongs_to Clause
      3. How's our schema looking?
    3. Making a Home for Each Story
      1. Determining Where a Story Lives
      2. Displaying Our Stories
      3. Improving the Story Randomizer
      4. Implementing Clean URLs
      5. Converting from Strings
      6. Investigating Link Generation
    4. Ajax, Pjax, and Turbolinks
      1. Introducing Ajax
    5. Making Stories
      1. Controlling Where the Votes Go
    6. The Asset Pipeline
      1. Why do we need an asset pipeline?
      2. Multiple Source Files
      3. Asset Preprocessors
      4. Asset Compression and Minification
      5. Asset Digests
    7. Get Out the Vote
      1. Styling the Scoreboard
      2. Response Formats
    8. Introducing Partials
      1. Adding Voting History
      2. Creating the Partial
      3. Styling the Voting History
      4. Tweaking the Voting History
    9. Testing the Voting Functionality
      1. Testing the Model
      2. Preparing the Fixtures
      3. Testing a Story's Relationship to a Vote
      4. Testing the Voting History Order
      5. Running the Unit Tests
    10. Testing the Controller
      1. Testing Page Rendering
      2. Testing Vote Storage
      3. Testing Ajax Voting
      4. Testing Regular HTTP Voting
    11. Running the Full Test Suite
    12. Summary
  17. Chapter 8: Protective Measures
    1. Introducing Sessions and Cookies
      1. Identifying Individual Users
      2. What's a cookie?
      3. What's a session?
      4. Sessions in Rails
    2. Modeling the User
      1. Generating a User Model
      2. Has Secure Password
      3. Adding Relationships for the User Class
      4. Creating a User
    3. Developing Login Functionality
      1. Creating the Controller
      2. Creating the View
      3. Adding Functionality to the Controller
    4. Introducing Filters
      1. Before Filters
      2. After Filter
      3. Around Filters
    5. Managing User Logins
      1. Retrieving the Current User
      2. Displaying the Name of the Current User
      3. Allowing Users to Log Out
    6. Adding a Navigation Menu
    7. Restricting the Application
      1. Protecting the Form
      2. Restricting Access to Story Submission
      3. Associating Stories with Users
      4. One Last Thing: Associate Votes to Users
    8. Testing User Authentication
      1. Testing the Model
      2. Testing the Controllers
      3. Fixing VotesController Tests
      4. Running the Full Test Suite
    9. Summary
  18. Chapter 9: Advanced Topics
    1. Promoting Popular Stories
      1. Using a Counter Cache
      2. Introducing the Counter Cache
      3. Making Room for the Cache
      4. Applying the Migration
    2. Implementing the Front Page
      1. Modifying the Controller
      2. Modifying the View
      3. Creating the Partial
      4. Styling the Front Page
      5. Setting the Default Page
    3. Implementing the Voting Bin
    4. Adding Custom Actions to RESTful Routes
    5. Abstracting Presentation Logic
      1. Avoiding Presentation Logic Spaghetti
      2. Introducing ActionView Helpers
      3. Writing an ActionView Helper
      4. Expanding the Navigation Menu
    6. Requiring a Login to Vote
    7. Auto-voting
      1. Introducing Model Callback
      2. Adding a Callback
    8. Adding a Description to Stories
      1. Adding a Model Attribute
      2. Expanding the Submission Form
      3. Whitelisting the New Attribute
    9. Adding User Pages
      1. Introducing the Join Model Relationship
      2. Introducing the has_many :through Association
      3. Adding Another Controller
      4. Creating the View
    10. Testing the New Functionality
      1. Testing the Model
      2. Testing the StoriesController
      3. Testing the VotesController
      4. Testing the UsersController
      5. Running the Complete Test Suite
    11. Summary
  19. Chapter 10: Rails Plugins
    1. What is a plugin?
    2. Adding Tagging to Readit
      1. Introducing the acts-as-taggable-on Gem
      2. Installing the acts-as-taggable-on Gem
      3. Creating a Migration for the Plugin
      4. Understanding Polymorphic Associations
      5. Making a Model Taggable
    3. Enabling Tag Submission
      1. Modifying the View
      2. Modifying the Controller
    4. Enabling Tag Display
      1. Modifying the View
      2. Updating the story Partial
    5. Assigning Our First Tags
    6. Viewing Stories
      1. Creating the Controller
      2. Filling in the View Template
      3. Displaying Tagged Stories
      4. Creating a tag Partial
      5. Updating the Stylesheet
    7. Testing the Tagging Functionality
      1. Testing the Model
      2. Testing the Controller
    8. Running the Test Suite ... Again!
    9. Summary
  20. Chapter 11: Debugging, Testing, and Benchmarking
    1. Debugging Your Application
      1. Debugging within Templates
      2. Web Console
      3. Debugging A Slightly Trickier Bug
    2. Using the Rails Logging Tool
    3. Overcoming Problems in Debugging
      1. Testing Your Application Using Integration Tests
      2. Using Breakpoints in a Test
      3. Revisiting the Rails Console
      4. A Brief Introduction to Pry
    4. Benchmarking Your Application
      1. Taking Benchmarks from Log Files
      2. Manual Benchmarking
    5. Summary
  21. Chapter 12: Deployment and Production Use
    1. The Implications of “Production”
    2. Choosing a Production Environment
    3. Web Servers
      1. Apache
      2. Nginx
    4. Application Servers
      1. Rack
      2. Terminology
      3. Application Servers for Rails
    5. Proxying Requests
    6. Software as a Service
      1. Heroku
    7. Alternatives for Session Storage
      1. The ActiveRecord Store Session Container
    8. Further Reading
      1. Caching
      2. ActionCable
      3. Rails API
      4. Performance
    9. Summary