Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

2267 lines (1427 sloc) 205.876 kB
<!DOCTYPE html>
<html>
<head>
<title>static-pages</title>
<link rel="stylesheet" href="pygments.css" type="text/css" />
<link rel="stylesheet" href="polytexnic.css" type="text/css" />
</head>
<body>
<div id="book">
<h1 class="title">Ruby on Rails Tutorial </h1>
<h1 class="subtitle"> Learn Web Development with Rails</h1>
<h2 class="author">Michael Hartl</h2>
<h2 class="contents">Contents</h2>
<div id="table_of_contents"><ol><li class="chapter"><a href="beginning.html#top"><span class="number">Chapter 1</span> From zero to deploy</a></li><li><ol><li class="section"><a href="beginning.html#sec-introduction"><span class="number">1.1</span> Introduction</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-comments_for_various_readers"><span class="number">1.1.1</span> Comments for various readers</a></li><li class="subsection"><a href="beginning.html#sec-1_1_2"><span class="number">1.1.2</span> &ldquo;Scaling&rdquo; Rails</a></li><li class="subsection"><a href="beginning.html#sec-conventions"><span class="number">1.1.3</span> Conventions in this book</a></li></ol></li><li class="section"><a href="beginning.html#sec-up_and_running"><span class="number">1.2</span> Up and running</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-development_tools"><span class="number">1.2.1</span> Development environments</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-1_2_1_1">IDEs</a></li><li class="subsubsection"><a href="beginning.html#sec-1_2_1_2">Text editors and command lines</a></li><li class="subsubsection"><a href="beginning.html#sec-1_2_1_3">Browsers</a></li><li class="subsubsection"><a href="beginning.html#sec-1_2_1_4">A note about tools</a></li></ol></li><li class="subsection"><a href="beginning.html#sec-rubygems"><span class="number">1.2.2</span> Ruby, RubyGems, Rails, and Git</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-rails_installer_windows">Rails Installer (Windows)</a></li><li class="subsubsection"><a href="beginning.html#sec-install_git">Install Git</a></li><li class="subsubsection"><a href="beginning.html#sec-install_ruby">Install Ruby</a></li><li class="subsubsection"><a href="beginning.html#sec-install_rubygems">Install RubyGems</a></li><li class="subsubsection"><a href="beginning.html#sec-install_rails">Install Rails</a></li></ol></li><li class="subsection"><a href="beginning.html#sec-the_first_application"><span class="number">1.2.3</span> The first application</a></li><li class="subsection"><a href="beginning.html#sec-bundler"><span class="number">1.2.4</span> Bundler</a></li><li class="subsection"><a href="beginning.html#sec-rails_server"><span class="number">1.2.5</span> <tt>rails server</tt></a></li><li class="subsection"><a href="beginning.html#sec-mvc"><span class="number">1.2.6</span> Model-view-controller (MVC)</a></li></ol></li><li class="section"><a href="beginning.html#sec-version_control"><span class="number">1.3</span> Version control with Git</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-git_setup"><span class="number">1.3.1</span> Installation and setup</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-1_3_1_1">First-time system setup</a></li><li class="subsubsection"><a href="beginning.html#sec-1_3_1_2">First-time repository setup</a></li></ol></li><li class="subsection"><a href="beginning.html#sec-adding_and_committing"><span class="number">1.3.2</span> Adding and committing</a></li><li class="subsection"><a href="beginning.html#sec-1_3_3"><span class="number">1.3.3</span> What good does Git do you?</a></li><li class="subsection"><a href="beginning.html#sec-github"><span class="number">1.3.4</span> GitHub</a></li><li class="subsection"><a href="beginning.html#sec-git_commands"><span class="number">1.3.5</span> Branch, edit, commit, merge</a></li><li><ol><li class="subsubsection"><a href="beginning.html#sec-git_branch">Branch</a></li><li class="subsubsection"><a href="beginning.html#sec-git_edit">Edit</a></li><li class="subsubsection"><a href="beginning.html#sec-git_commit">Commit</a></li><li class="subsubsection"><a href="beginning.html#sec-git_merge">Merge</a></li><li class="subsubsection"><a href="beginning.html#sec-git_push">Push</a></li></ol></li></ol></li><li class="section"><a href="beginning.html#sec-deploying"><span class="number">1.4</span> Deploying</a></li><li><ol><li class="subsection"><a href="beginning.html#sec-heroku_setup"><span class="number">1.4.1</span> Heroku setup</a></li><li class="subsection"><a href="beginning.html#sec-heroku_step_one"><span class="number">1.4.2</span> Heroku deployment, step one</a></li><li class="subsection"><a href="beginning.html#sec-1_4_3"><span class="number">1.4.3</span> Heroku deployment, step two</a></li><li class="subsection"><a href="beginning.html#sec-heroku_commands"><span class="number">1.4.4</span> Heroku commands</a></li></ol></li><li class="section"><a href="beginning.html#sec-beginning_conclusion"><span class="number">1.5</span> Conclusion</a></li></ol></li><li class="chapter"><a href="a-demo-app.html#top"><span class="number">Chapter 2</span> A demo app</a></li><li><ol><li class="section"><a href="a-demo-app.html#sec-planning_the_application"><span class="number">2.1</span> Planning the application</a></li><li><ol><li class="subsection"><a href="a-demo-app.html#sec-modeling_demo_users"><span class="number">2.1.1</span> Modeling demo users</a></li><li class="subsection"><a href="a-demo-app.html#sec-modeling_demo_microposts"><span class="number">2.1.2</span> Modeling demo microposts</a></li></ol></li><li class="section"><a href="a-demo-app.html#sec-demo_users_resource"><span class="number">2.2</span> The Users resource</a></li><li><ol><li class="subsection"><a href="a-demo-app.html#sec-a_user_tour"><span class="number">2.2.1</span> A user tour</a></li><li class="subsection"><a href="a-demo-app.html#sec-mvc_in_action"><span class="number">2.2.2</span> MVC in action</a></li><li class="subsection"><a href="a-demo-app.html#sec-weaknesses_of_this_users_resource"><span class="number">2.2.3</span> Weaknesses of this Users resource</a></li></ol></li><li class="section"><a href="a-demo-app.html#sec-microposts_resource"><span class="number">2.3</span> The Microposts resource</a></li><li><ol><li class="subsection"><a href="a-demo-app.html#sec-a_micropost_microtour"><span class="number">2.3.1</span> A micropost microtour</a></li><li class="subsection"><a href="a-demo-app.html#sec-putting_the_micro_in_microposts"><span class="number">2.3.2</span> Putting the <em>micro</em> in microposts</a></li><li class="subsection"><a href="a-demo-app.html#sec-demo_user_has_many_microposts"><span class="number">2.3.3</span> A user <tt>has_many</tt> microposts</a></li><li class="subsection"><a href="a-demo-app.html#sec-inheritance_hierarchies"><span class="number">2.3.4</span> Inheritance hierarchies</a></li><li class="subsection"><a href="a-demo-app.html#sec-deploying_the_demo_app"><span class="number">2.3.5</span> Deploying the demo app</a></li></ol></li><li class="section"><a href="a-demo-app.html#sec-2_4"><span class="number">2.4</span> Conclusion</a></li></ol></li><li class="chapter"><a href="static-pages.html#top"><span class="number">Chapter 3</span> Mostly static pages</a></li><li><ol><li class="section"><a href="static-pages.html#sec-static_pages"><span class="number">3.1</span> Static pages</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-truly_static_pages"><span class="number">3.1.1</span> Truly static pages</a></li><li class="subsection"><a href="static-pages.html#sec-static_pages_with_rails"><span class="number">3.1.2</span> Static pages with Rails</a></li></ol></li><li class="section"><a href="static-pages.html#sec-first_tests"><span class="number">3.2</span> Our first tests</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-TDD"><span class="number">3.2.1</span> Test-driven development</a></li><li class="subsection"><a href="static-pages.html#sec-adding_a_page"><span class="number">3.2.2</span> Adding a page</a></li><li><ol><li class="subsubsection"><a href="static-pages.html#sec-red">Red</a></li><li class="subsubsection"><a href="static-pages.html#sec-green">Green</a></li><li class="subsubsection"><a href="static-pages.html#sec-refactor">Refactor</a></li></ol></li></ol></li><li class="section"><a href="static-pages.html#sec-slightly_dynamic_pages"><span class="number">3.3</span> Slightly dynamic pages</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-testing_a_title_change"><span class="number">3.3.1</span> Testing a title change</a></li><li class="subsection"><a href="static-pages.html#sec-passing_title_tests"><span class="number">3.3.2</span> Passing title tests</a></li><li class="subsection"><a href="static-pages.html#sec-embedded_ruby"><span class="number">3.3.3</span> Embedded Ruby</a></li><li class="subsection"><a href="static-pages.html#sec-layouts"><span class="number">3.3.4</span> Eliminating duplication with layouts</a></li></ol></li><li class="section"><a href="static-pages.html#sec-static_pages_conclusion"><span class="number">3.4</span> Conclusion</a></li><li class="section"><a href="static-pages.html#sec-static_pages_exercises"><span class="number">3.5</span> Exercises</a></li><li class="section"><a href="static-pages.html#sec-advanced_setup"><span class="number">3.6</span> Advanced setup</a></li><li><ol><li class="subsection"><a href="static-pages.html#sec-eliminating_bundle_exec"><span class="number">3.6.1</span> Eliminating <tt>bundle exec</tt></a></li><li><ol><li class="subsubsection"><a href="static-pages.html#sec-rvm_bundler_integration">RVM Bundler integration</a></li><li class="subsubsection"><a href="static-pages.html#sec-binstubs">binstubs</a></li></ol></li><li class="subsection"><a href="static-pages.html#sec-guard"><span class="number">3.6.2</span> Automated tests with Guard</a></li><li class="subsection"><a href="static-pages.html#sec-spork"><span class="number">3.6.3</span> Speeding up tests with Spork</a></li><li><ol><li class="subsubsection"><a href="static-pages.html#sec-spork_and_guard">Guard with Spork</a></li></ol></li><li class="subsection"><a href="static-pages.html#sec-tests_inside_sublime_text"><span class="number">3.6.4</span> Tests inside Sublime Text</a></li></ol></li></ol></li><li class="chapter"><a href="rails-flavored-ruby.html#top"><span class="number">Chapter 4</span> Rails-flavored Ruby</a></li><li><ol><li class="section"><a href="rails-flavored-ruby.html#sec-motivation"><span class="number">4.1</span> Motivation</a></li><li class="section"><a href="rails-flavored-ruby.html#sec-strings_and_methods"><span class="number">4.2</span> Strings and methods</a></li><li><ol><li class="subsection"><a href="rails-flavored-ruby.html#sec-comments"><span class="number">4.2.1</span> Comments</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-strings"><span class="number">4.2.2</span> Strings</a></li><li><ol><li class="subsubsection"><a href="rails-flavored-ruby.html#sec-printing">Printing</a></li><li class="subsubsection"><a href="rails-flavored-ruby.html#sec-single_quoted_strings">Single-quoted strings</a></li></ol></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-objects_and_message_passing"><span class="number">4.2.3</span> Objects and message passing</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-method_definitions"><span class="number">4.2.4</span> Method definitions</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-back_to_the_title_helper"><span class="number">4.2.5</span> Back to the title helper</a></li></ol></li><li class="section"><a href="rails-flavored-ruby.html#sec-other_data_structures"><span class="number">4.3</span> Other data structures</a></li><li><ol><li class="subsection"><a href="rails-flavored-ruby.html#sec-arrays_and_ranges"><span class="number">4.3.1</span> Arrays and ranges</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-blocks"><span class="number">4.3.2</span> Blocks</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-hashes_and_symbols"><span class="number">4.3.3</span> Hashes and symbols</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-css_revisited"><span class="number">4.3.4</span> CSS revisited</a></li></ol></li><li class="section"><a href="rails-flavored-ruby.html#sec-ruby_classes"><span class="number">4.4</span> Ruby classes</a></li><li><ol><li class="subsection"><a href="rails-flavored-ruby.html#sec-constructors"><span class="number">4.4.1</span> Constructors</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-a_class_of_our_own"><span class="number">4.4.2</span> Class inheritance</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-modifying_built_in_classes"><span class="number">4.4.3</span> Modifying built-in classes</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-a_controller_class"><span class="number">4.4.4</span> A controller class</a></li><li class="subsection"><a href="rails-flavored-ruby.html#sec-a_user_class"><span class="number">4.4.5</span> A user class</a></li></ol></li><li class="section"><a href="rails-flavored-ruby.html#sec-conclusion"><span class="number">4.5</span> Conclusion</a></li><li class="section"><a href="rails-flavored-ruby.html#sec-exercises"><span class="number">4.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="filling-in-the-layout.html#top"><span class="number">Chapter 5</span> Filling in the layout</a></li><li><ol><li class="section"><a href="filling-in-the-layout.html#sec-structure"><span class="number">5.1</span> Adding some structure</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-adding_to_the_layout"><span class="number">5.1.1</span> Site navigation</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-custom_css"><span class="number">5.1.2</span> Bootstrap and custom CSS</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-partials"><span class="number">5.1.3</span> Partials</a></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-sass_and_the_asset_pipeline"><span class="number">5.2</span> Sass and the asset pipeline</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-the_asset_pipeline"><span class="number">5.2.1</span> The asset pipeline</a></li><li><ol><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_1">Asset directories</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_2">Manifest files</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_3">Preprocessor engines</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_1_4">Efficiency in production</a></li></ol></li><li class="subsection"><a href="filling-in-the-layout.html#sec-sass"><span class="number">5.2.2</span> Syntactically awesome stylesheets</a></li><li><ol><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_2_1">Nesting</a></li><li class="subsubsection"><a href="filling-in-the-layout.html#sec-5_2_2_2">Variables</a></li></ol></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-layout_links"><span class="number">5.3</span> Layout links</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-route_tests"><span class="number">5.3.1</span> Route tests</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-rails_routes"><span class="number">5.3.2</span> Rails routes</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-named_routes"><span class="number">5.3.3</span> Named routes</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-pretty_rspec"><span class="number">5.3.4</span> Pretty RSpec</a></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-user_signup"><span class="number">5.4</span> User signup: A first step</a></li><li><ol><li class="subsection"><a href="filling-in-the-layout.html#sec-users_controller"><span class="number">5.4.1</span> Users controller</a></li><li class="subsection"><a href="filling-in-the-layout.html#sec-signup_url"><span class="number">5.4.2</span> Signup URI</a></li></ol></li><li class="section"><a href="filling-in-the-layout.html#sec-layout_conclusion"><span class="number">5.5</span> Conclusion</a></li><li class="section"><a href="filling-in-the-layout.html#sec-layout_exercises"><span class="number">5.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="modeling-users.html#top"><span class="number">Chapter 6</span> Modeling users</a></li><li><ol><li class="section"><a href="modeling-users.html#sec-user_model"><span class="number">6.1</span> User model</a></li><li><ol><li class="subsection"><a href="modeling-users.html#sec-database_migrations"><span class="number">6.1.1</span> Database migrations</a></li><li class="subsection"><a href="modeling-users.html#sec-the_model_file"><span class="number">6.1.2</span> The model file</a></li><li><ol><li class="subsubsection"><a href="modeling-users.html#sec-model_annotation">Model annotation</a></li><li class="subsubsection"><a href="modeling-users.html#sec-accessible_attributes">Accessible attributes</a></li></ol></li><li class="subsection"><a href="modeling-users.html#sec-creating_user_objects"><span class="number">6.1.3</span> Creating user objects</a></li><li class="subsection"><a href="modeling-users.html#sec-finding_user_objects"><span class="number">6.1.4</span> Finding user objects</a></li><li class="subsection"><a href="modeling-users.html#sec-updating_user_objects"><span class="number">6.1.5</span> Updating user objects</a></li></ol></li><li class="section"><a href="modeling-users.html#sec-user_validations"><span class="number">6.2</span> User validations</a></li><li><ol><li class="subsection"><a href="modeling-users.html#sec-initial_user_tests"><span class="number">6.2.1</span> Initial user tests</a></li><li class="subsection"><a href="modeling-users.html#sec-presence_validation"><span class="number">6.2.2</span> Validating presence</a></li><li class="subsection"><a href="modeling-users.html#sec-length_validation"><span class="number">6.2.3</span> Length validation</a></li><li class="subsection"><a href="modeling-users.html#sec-format_validation"><span class="number">6.2.4</span> Format validation</a></li><li class="subsection"><a href="modeling-users.html#sec-uniqueness_validation"><span class="number">6.2.5</span> Uniqueness validation</a></li><li><ol><li class="subsubsection"><a href="modeling-users.html#sec-the_caveat">The uniqueness caveat</a></li></ol></li></ol></li><li class="section"><a href="modeling-users.html#sec-adding_a_secure_password"><span class="number">6.3</span> Adding a secure password</a></li><li><ol><li class="subsection"><a href="modeling-users.html#sec-an_encrypted_password"><span class="number">6.3.1</span> An encrypted password</a></li><li class="subsection"><a href="modeling-users.html#sec-password_and_confirmation"><span class="number">6.3.2</span> Password and confirmation</a></li><li class="subsection"><a href="modeling-users.html#sec-user_authentication"><span class="number">6.3.3</span> User authentication</a></li><li class="subsection"><a href="modeling-users.html#sec-has_secure_password"><span class="number">6.3.4</span> User has secure password</a></li><li class="subsection"><a href="modeling-users.html#sec-creating_a_user"><span class="number">6.3.5</span> Creating a user</a></li></ol></li><li class="section"><a href="modeling-users.html#sec-6_4"><span class="number">6.4</span> Conclusion</a></li><li class="section"><a href="modeling-users.html#sec-6_5"><span class="number">6.5</span> Exercises</a></li></ol></li><li class="chapter"><a href="sign-up.html#top"><span class="number">Chapter 7</span> Sign up</a></li><li><ol><li class="section"><a href="sign-up.html#sec-showing_users"><span class="number">7.1</span> Showing users</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-rails_environments"><span class="number">7.1.1</span> Debug and Rails environments</a></li><li class="subsection"><a href="sign-up.html#sec-a_users_resource"><span class="number">7.1.2</span> A Users resource</a></li><li class="subsection"><a href="sign-up.html#sec-tests_with_factories"><span class="number">7.1.3</span> Testing the user show page (with factories)</a></li><li class="subsection"><a href="sign-up.html#sec-a_gravatar_image"><span class="number">7.1.4</span> A Gravatar image and a sidebar</a></li></ol></li><li class="section"><a href="sign-up.html#sec-signup_form"><span class="number">7.2</span> Signup form</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-tests_for_user_signup"><span class="number">7.2.1</span> Tests for user signup</a></li><li class="subsection"><a href="sign-up.html#sec-using_form_for"><span class="number">7.2.2</span> Using <tt>form_for</tt></a></li><li class="subsection"><a href="sign-up.html#sec-the_form_html"><span class="number">7.2.3</span> The form HTML</a></li></ol></li><li class="section"><a href="sign-up.html#sec-signup_failure"><span class="number">7.3</span> Signup failure</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-a_working_form"><span class="number">7.3.1</span> A working form</a></li><li class="subsection"><a href="sign-up.html#sec-signup_error_messages"><span class="number">7.3.2</span> Signup error messages</a></li></ol></li><li class="section"><a href="sign-up.html#sec-signup_success"><span class="number">7.4</span> Signup success</a></li><li><ol><li class="subsection"><a href="sign-up.html#sec-the_finished_signup_form"><span class="number">7.4.1</span> The finished signup form</a></li><li class="subsection"><a href="sign-up.html#sec-the_flash"><span class="number">7.4.2</span> The flash</a></li><li class="subsection"><a href="sign-up.html#sec-the_first_signup"><span class="number">7.4.3</span> The first signup</a></li><li class="subsection"><a href="sign-up.html#sec-deploying_to_production_with_ssl"><span class="number">7.4.4</span> Deploying to production with SSL</a></li></ol></li><li class="section"><a href="sign-up.html#sec-7_5"><span class="number">7.5</span> Conclusion</a></li><li class="section"><a href="sign-up.html#sec-signup_exercises"><span class="number">7.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="sign-in-sign-out.html#top"><span class="number">Chapter 8</span> Sign in, sign out</a></li><li><ol><li class="section"><a href="sign-in-sign-out.html#sec-signin_failure"><span class="number">8.1</span> Sessions and signin failure</a></li><li><ol><li class="subsection"><a href="sign-in-sign-out.html#sec-sessions_controller"><span class="number">8.1.1</span> Sessions controller</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signin_tests"><span class="number">8.1.2</span> Signin tests</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signin_form"><span class="number">8.1.3</span> Signin form</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-reviewing_form_submission"><span class="number">8.1.4</span> Reviewing form submission</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-rendering_with_a_flash_message"><span class="number">8.1.5</span> Rendering with a flash message</a></li></ol></li><li class="section"><a href="sign-in-sign-out.html#sec-signin_success"><span class="number">8.2</span> Signin success</a></li><li><ol><li class="subsection"><a href="sign-in-sign-out.html#sec-remember_me"><span class="number">8.2.1</span> Remember me</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-a_working_sign_in_method"><span class="number">8.2.2</span> A working <tt>sign_in</tt> method</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-current_user"><span class="number">8.2.3</span> Current user</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-changing_the_layout_links"><span class="number">8.2.4</span> Changing the layout links</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signin_upon_signup"><span class="number">8.2.5</span> Signin upon signup</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-signing_out"><span class="number">8.2.6</span> Signing out</a></li></ol></li><li class="section"><a href="sign-in-sign-out.html#sec-cucumber"><span class="number">8.3</span> Introduction to Cucumber (optional)</a></li><li><ol><li class="subsection"><a href="sign-in-sign-out.html#sec-installation_and_setup"><span class="number">8.3.1</span> Installation and setup</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-features_and_steps"><span class="number">8.3.2</span> Features and steps</a></li><li class="subsection"><a href="sign-in-sign-out.html#sec-rspec_custom_matchers"><span class="number">8.3.3</span> Counterpoint: RSpec custom matchers</a></li></ol></li><li class="section"><a href="sign-in-sign-out.html#sec-8_4"><span class="number">8.4</span> Conclusion</a></li><li class="section"><a href="sign-in-sign-out.html#sec-sign_in_out_exercises"><span class="number">8.5</span> Exercises</a></li></ol></li><li class="chapter"><a href="updating-showing-and-deleting-users.html#top"><span class="number">Chapter 9</span> Updating, showing, and deleting users</a></li><li><ol><li class="section"><a href="updating-showing-and-deleting-users.html#sec-updating_users"><span class="number">9.1</span> Updating users</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-edit_form"><span class="number">9.1.1</span> Edit form</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-unsuccessful_edits"><span class="number">9.1.2</span> Unsuccessful edits</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-successful_edits"><span class="number">9.1.3</span> Successful edits</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-authorization"><span class="number">9.2</span> Authorization</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-requiring_signed_in_users"><span class="number">9.2.1</span> Requiring signed-in users</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-requiring_the_right_user"><span class="number">9.2.2</span> Requiring the right user</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-friendly_forwarding"><span class="number">9.2.3</span> Friendly forwarding</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-showing_all_users"><span class="number">9.3</span> Showing all users</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-user_index"><span class="number">9.3.1</span> User index</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-sample_users"><span class="number">9.3.2</span> Sample users</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-pagination"><span class="number">9.3.3</span> Pagination</a></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-partial_refactoring"><span class="number">9.3.4</span> Partial refactoring</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-destroying_users"><span class="number">9.4</span> Deleting users</a></li><li><ol><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-administrative_users"><span class="number">9.4.1</span> Administrative users</a></li><li><ol><li class="subsubsection"><a href="updating-showing-and-deleting-users.html#sec-revisiting_attr_accessible">Revisiting <tt>attr_accessible</tt></a></li></ol></li><li class="subsection"><a href="updating-showing-and-deleting-users.html#sec-the_destroy_action"><span class="number">9.4.2</span> The <tt>destroy</tt> action</a></li></ol></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-updating_and_deleting_users_conclusion"><span class="number">9.5</span> Conclusion</a></li><li class="section"><a href="updating-showing-and-deleting-users.html#sec-updating_deleting_exercises"><span class="number">9.6</span> Exercises</a></li></ol></li><li class="chapter"><a href="user-microposts.html#top"><span class="number">Chapter 10</span> User microposts</a></li><li><ol><li class="section"><a href="user-microposts.html#sec-a_micropost_model"><span class="number">10.1</span> A Micropost model</a></li><li><ol><li class="subsection"><a href="user-microposts.html#sec-the_basic_model"><span class="number">10.1.1</span> The basic model</a></li><li class="subsection"><a href="user-microposts.html#sec-accessible_attribute"><span class="number">10.1.2</span> Accessible attributes and the first validation</a></li><li class="subsection"><a href="user-microposts.html#sec-user_micropost_associations"><span class="number">10.1.3</span> User/Micropost associations</a></li><li class="subsection"><a href="user-microposts.html#sec-ordering_and_dependency"><span class="number">10.1.4</span> Micropost refinements</a></li><li><ol><li class="subsubsection"><a href="user-microposts.html#sec-default_scope">Default scope</a></li><li class="subsubsection"><a href="user-microposts.html#sec-dependent_destroy">Dependent: destroy</a></li></ol></li><li class="subsection"><a href="user-microposts.html#sec-micropost_validations"><span class="number">10.1.5</span> Content validations</a></li></ol></li><li class="section"><a href="user-microposts.html#sec-showing_microposts"><span class="number">10.2</span> Showing microposts</a></li><li><ol><li class="subsection"><a href="user-microposts.html#sec-augmenting_the_user_show_page"><span class="number">10.2.1</span> Augmenting the user show page</a></li><li class="subsection"><a href="user-microposts.html#sec-sample_microposts"><span class="number">10.2.2</span> Sample microposts</a></li></ol></li><li class="section"><a href="user-microposts.html#sec-manipulating_microposts"><span class="number">10.3</span> Manipulating microposts</a></li><li><ol><li class="subsection"><a href="user-microposts.html#sec-access_control"><span class="number">10.3.1</span> Access control</a></li><li class="subsection"><a href="user-microposts.html#sec-creating_microposts"><span class="number">10.3.2</span> Creating microposts</a></li><li class="subsection"><a href="user-microposts.html#sec-a_proto_feed"><span class="number">10.3.3</span> A proto-feed</a></li><li class="subsection"><a href="user-microposts.html#sec-destroying_microposts"><span class="number">10.3.4</span> Destroying microposts</a></li></ol></li><li class="section"><a href="user-microposts.html#sec-10_4"><span class="number">10.4</span> Conclusion</a></li><li class="section"><a href="user-microposts.html#sec-micropost_exercises"><span class="number">10.5</span> Exercises</a></li></ol></li><li class="chapter"><a href="following-users.html#top"><span class="number">Chapter 11</span> Following users</a></li><li><ol><li class="section"><a href="following-users.html#sec-the_relationship_model"><span class="number">11.1</span> The Relationship model</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-a_problem_with_the_data_model"><span class="number">11.1.1</span> A problem with the data model (and a solution)</a></li><li class="subsection"><a href="following-users.html#sec-relationship_user_associations"><span class="number">11.1.2</span> User/relationship associations</a></li><li class="subsection"><a href="following-users.html#sec-relationship_validations"><span class="number">11.1.3</span> Validations</a></li><li class="subsection"><a href="following-users.html#sec-following"><span class="number">11.1.4</span> Followed users</a></li><li class="subsection"><a href="following-users.html#sec-followers"><span class="number">11.1.5</span> Followers</a></li></ol></li><li class="section"><a href="following-users.html#sec-a_web_interface_for_following_and_followers"><span class="number">11.2</span> A web interface for following users</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-sample_following_data"><span class="number">11.2.1</span> Sample following data</a></li><li class="subsection"><a href="following-users.html#sec-stats_and_a_follow_form"><span class="number">11.2.2</span> Stats and a follow form</a></li><li class="subsection"><a href="following-users.html#sec-following_and_followers_pages"><span class="number">11.2.3</span> Following and followers pages</a></li><li class="subsection"><a href="following-users.html#sec-a_working_follow_button_the_standard_way"><span class="number">11.2.4</span> A working follow button the standard way</a></li><li class="subsection"><a href="following-users.html#sec-a_working_follow_button_with_ajax"><span class="number">11.2.5</span> A working follow button with Ajax</a></li></ol></li><li class="section"><a href="following-users.html#sec-the_status_feed"><span class="number">11.3</span> The status feed</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-motivation_and_strategy"><span class="number">11.3.1</span> Motivation and strategy</a></li><li class="subsection"><a href="following-users.html#sec-a_first_feed_implementation"><span class="number">11.3.2</span> A first feed implementation</a></li><li class="subsection"><a href="following-users.html#sec-scopes_subselects_and_a_lambda"><span class="number">11.3.3</span> Subselects</a></li><li class="subsection"><a href="following-users.html#sec-the_new_status_feed"><span class="number">11.3.4</span> The new status feed</a></li></ol></li><li class="section"><a href="following-users.html#sec-following_conclusion"><span class="number">11.4</span> Conclusion</a></li><li><ol><li class="subsection"><a href="following-users.html#sec-extensions_to_the_sample_application"><span class="number">11.4.1</span> Extensions to the sample application</a></li><li><ol><li class="subsubsection"><a href="following-users.html#sec-replies">Replies</a></li><li class="subsubsection"><a href="following-users.html#sec-messaging">Messaging</a></li><li class="subsubsection"><a href="following-users.html#sec-follower_notifications">Follower notifications</a></li><li class="subsubsection"><a href="following-users.html#sec-password_reminders">Password reminders</a></li><li class="subsubsection"><a href="following-users.html#sec-signup_confirmation">Signup confirmation</a></li><li class="subsubsection"><a href="following-users.html#sec-rss_feed">RSS feed</a></li><li class="subsubsection"><a href="following-users.html#sec-rest_api">REST API</a></li><li class="subsubsection"><a href="following-users.html#sec-search">Search</a></li></ol></li><li class="subsection"><a href="following-users.html#sec-guide_to_further_resources"><span class="number">11.4.2</span> Guide to further resources</a></li></ol></li><li class="section"><a href="following-users.html#sec-following_exercises"><span class="number">11.5</span> Exercises</a></li></ol></li></ol></div>
<div id="main_content"></div>
<p> <span class="preamble">
<span id="foreword">
<strong> Foreword</strong> <br />
</span>
</span></p>
<p>My former company (CD Baby) was one of the first to loudly switch to Ruby on Rails, and then even more loudly switch back to PHP (Google me to read about the drama). This book by Michael Hartl came so highly recommended that I had to try it, and the <em>Ruby on Rails Tutorial</em> is what I used to switch back to Rails again.</p>
<p>Though I&rsquo;ve worked my way through many Rails books, this is the one that finally made me &ldquo;get&rdquo; it. Everything is done very much &ldquo;the Rails way&rdquo;&mdash;a way that felt very unnatural to me before, but now after doing this book finally feels natural. This is also the only Rails book that does test-driven development the entire time, an approach highly recommended by the experts but which has never been so clearly demonstrated before. Finally, by including Git, GitHub, and Heroku in the demo examples, the author really gives you a feel for what it&rsquo;s like to do a real-world project. The tutorial&rsquo;s code examples are not in isolation.</p>
<p>The linear narrative is such a great format. Personally, I powered through the <em>Rails Tutorial</em> in three long days, doing all the examples and challenges at the end of each chapter. Do it from start to finish, without jumping around, and you&rsquo;ll get the ultimate benefit.</p>
<p>Enjoy!</p>
<p><a href="http://sivers.org/">Derek Sivers</a> (<a href="http://sivers.org/">sivers.org</a>) <br />
<em>Formerly: Founder,</em> <a href="http://www.cdbaby.com/"><em>CD Baby</em></a> <br />
<em>Currently: Founder,</em> <a href="http://thoughts.pro/"><em>Thoughts Ltd.</em></a> <br /></p>
<p> <span class="preamble">
<strong> Acknowledgments</strong> <br />
</span></p>
<p>The <em>Ruby on Rails Tutorial</em> owes a lot to my previous Rails book, <em>RailsSpace</em>, and hence to my coauthor <a href="http://aure.com/">Aurelius Prochazka</a>. I&rsquo;d like to thank Aure both for the work he did on that book and for his support of this one. I&rsquo;d also like to thank Debra Williams Cauley, my editor on both <em>RailsSpace</em> and the <em>Ruby on Rails Tutorial</em>; as long as she keeps taking me to baseball games, I&rsquo;ll keep writing books for her.</p>
<p>I&rsquo;d like to acknowledge a long list of Rubyists who have taught and inspired me over the years: David Heinemeier Hansson, Yehuda Katz, Carl Lerche, Jeremy Kemper, Xavier Noria, Ryan Bates, Geoffrey Grosenbach, Peter Cooper, Matt Aimonetti, Gregg Pollack, Wayne&nbsp;E. Seguin, Amy Hoy, Dave Chelimsky, Pat Maddox, Tom Preston-Werner, Chris Wanstrath, Chad Fowler, Josh Susser, Obie Fernandez, Ian McFarland, Steven Bristol, Wolfram Arnold, Alex Chaffee, Giles Bowkett, Evan Dorn, Long Nguyen, James Lindenbaum, Adam Wiggins, Tikhon Bernstam, Ron Evans, Wyatt Greene, Miles Forrest, the good people at Pivotal Labs, the Heroku gang, the thoughtbot guys, and the GitHub crew. Finally, many, many readers&mdash;far too many to list&mdash;have contributed a huge number of bug reports and suggestions during the writing of this book, and I gratefully acknowledge their help in making it as good as it can&nbsp;be. <br /></p>
<p> <span class="preamble">
<span id="author">
<strong> About the author</strong> <br />
</span>
</span></p>
<p><a href="http://michaelhartl.com/">Michael Hartl</a> is the author of the <a href="http://ruby.railstutorial.org/"><em>Ruby on Rails Tutorial</em></a>, the leading introduction to web development with <a href="http://rubyonrails.org/">Ruby on Rails</a>. His prior experience includes writing and developing <em>RailsSpace</em>, an extremely obsolete Rails tutorial book, and developing Insoshi, a once-popular and now-obsolete social networking platform in Ruby on Rails. In 2011, Michael received a <a href="http://rubyheroes.com/heroes">Ruby Hero Award</a> for his contributions to the Ruby community. He is a graduate of <a href="http://college.harvard.edu/">Harvard College</a>, has a <a href="http://resolver.caltech.edu/CaltechETD:etd-05222003-161626">Ph.D. in Physics</a> from <a href="http://www.caltech.edu/">Caltech</a>, and is an alumnus of the <a href="http://ycombinator.com/">Y&nbsp;Combinator</a> entrepreneur program. <br /></p>
<p> <span id="license" class="preamble">
<strong> Copyright and license</strong> <br />
</span></p>
<p><em>Ruby on Rails Tutorial: Learn Web Devlopment with Rails</em>. Copyright &copy; 2012 by Michael Hartl. All source code in the <em>Ruby on Rails Tutorial</em> is available jointly under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> and the <a href="http://people.freebsd.org/~phk/">Beerware License</a>.</p>
<div class="code"><div class="highlight"><pre>The MIT License
Copyright (c) 2012 Michael Hartl
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the &quot;Software&quot;), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
</pre></div>
</div>
<div class="code"><div class="highlight"><pre>/*
* ----------------------------------------------------------------------------
* &quot;THE BEER-WARE LICENSE&quot; (Revision 42):
* Michael Hartl wrote this code. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return.
* ----------------------------------------------------------------------------
*/
</pre></div>
</div>
<div id="top"></div>
<h1 class="chapter"><a id="sec-3" href="static-pages.html#top" class="heading"><span class="number">Chapter 3</span> Mostly static pages</a></h1>
<p>In this chapter, we will begin developing the sample application that will serve as our example throughout the rest of this tutorial. Although the sample app will eventually have users, microposts, and a full login and authentication framework, we will begin with a seemingly limited topic: the creation of static pages. Despite its apparent simplicity, making static pages is a highly instructive exercise, rich in implications&mdash;a perfect start for our nascent application.</p>
<p>Although Rails is designed for making database-backed dynamic websites, it also excels at making the kind of static pages we might make with raw HTML files. In fact, using Rails even for static pages yields a distinct advantage: we can easily add just a <em>small</em> amount of dynamic content. In this chapter we&rsquo;ll learn how. Along the way, we&rsquo;ll get our first taste of <em>automated testing</em>, which will help us be more confident that our code is correct. Moreover, having a good test suite will allow us to <em>refactor</em> our code with confidence, changing its form without changing its function.</p>
<p>There&rsquo;s a lot of code in this chapter, especially in <a class="ref" href="static-pages.html#sec-first_tests">Section&nbsp;3.2</a> and <a class="ref" href="static-pages.html#sec-slightly_dynamic_pages">Section&nbsp;3.3</a>, and if you&rsquo;re new to Ruby you shouldn&rsquo;t worry about understanding the details right now. As noted in <a class="ref" href="beginning.html#sec-comments_for_various_readers">Section&nbsp;1.1.1</a>, one strategy is to copy-and-paste the tests and use them to verify the application code, without worrying at this point how they work. In addition, <a class="ref" href="rails-flavored-ruby.html#top">Chapter&nbsp;4</a> covers Ruby in more depth, so there is plenty of opportunity for these ideas to sink in. Finally, RSpec tests will recur throughout the tutorial, so if you get stuck now I recommend forging ahead; you&rsquo;ll be amazed how, after just a few more chapters, initially inscrutable code will suddenly look simple.</p>
<p>As in <a class="ref" href="a-demo-app.html#top">Chapter&nbsp;2</a>, before getting started we need to create a new Rails project, this time called <code>sample_app</code>:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> <span class="nb">cd</span> ~/rails_projects
<span class="gp">$</span> rails new sample_app --skip-test-unit
<span class="gp">$</span> <span class="nb">cd </span>sample_app
</pre></div>
</div>
<p>Here the <tt class="verb">--skip-test-unit</tt> option to the <code>rails</code> command tells Rails not to generate a <code>test</code> directory associated with the default <code>Test::Unit</code> framework. This is not because we won&rsquo;t be writing tests; on the contrary, starting in <a class="ref" href="static-pages.html#sec-first_tests">Section&nbsp;3.2</a> we will be using an alternate testing framework called <em>RSpec</em> to write a thorough test suite.</p>
<p>As in <a class="ref" href="a-demo-app.html#sec-planning_the_application">Section&nbsp;2.1</a>, our next step is to use a text editor to update the <code>Gemfile</code> with the gems needed by our application. On the other hand, for the sample application we&rsquo;ll also need two gems we didn&rsquo;t need before: the gem for RSpec and the gem for the RSpec library specific to Rails. The code to include them is shown in <a class="ref" href="static-pages.html#code-gemfile_rspec">Listing&nbsp;3.1</a>. (<em>Note</em>: If you would like to install <em>all</em> the gems needed for the sample application, you should use the code in <a class="ref" href="updating-showing-and-deleting-users.html#code-final_gemfile">Listing&nbsp;9.49</a> at this time.)</p>
<div class="label" id="code-gemfile_rspec"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.1.</span> <span class="description">A <code>Gemfile</code> for the sample app.</span> </div>
<div class="code"><div class="highlight"><pre><span class="n">source</span> <span class="s1">&#39;https://rubygems.org&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;rails&#39;</span><span class="p">,</span> <span class="s1">&#39;3.2.8&#39;</span>
<span class="n">group</span> <span class="ss">:development</span><span class="p">,</span> <span class="ss">:test</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;sqlite3&#39;</span><span class="p">,</span> <span class="s1">&#39;1.3.5&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;rspec-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;2.11.0&#39;</span>
<span class="k">end</span>
<span class="c1"># Gems used only for assets and not required</span>
<span class="c1"># in production environments by default.</span>
<span class="n">group</span> <span class="ss">:assets</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;sass-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;3.2.5&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;coffee-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;3.2.2&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;uglifier&#39;</span><span class="p">,</span> <span class="s1">&#39;1.2.3&#39;</span>
<span class="k">end</span>
<span class="n">gem</span> <span class="s1">&#39;jquery-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;2.0.2&#39;</span>
<span class="n">group</span> <span class="ss">:test</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;capybara&#39;</span><span class="p">,</span> <span class="s1">&#39;1.1.2&#39;</span>
<span class="k">end</span>
<span class="n">group</span> <span class="ss">:production</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;pg&#39;</span><span class="p">,</span> <span class="s1">&#39;0.12.2&#39;</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>This includes <tt>rspec-rails</tt> in development mode so that we have access to RSpec-specific generators, and it includes it in test mode in order to run the tests. We don&rsquo;t have to install RSpec itself because it is a dependency of <tt>rspec-rails</tt> and will thus be installed automatically. We also include the <a href="https://github.com/jnicklas/capybara">Capybara gem</a>, which allows us to simulate a user&rsquo;s interaction with the sample application using a natural English-like syntax.<sup class="footnote" id="fnref-3_1"><a href="#fn-3_1">1</a></sup> As in <a class="ref" href="a-demo-app.html#top">Chapter&nbsp;2</a>, we also must include the PostgreSQL gem in production for deployment to Heroku:</p>
<div class="code"><div class="highlight"><pre><span class="n">group</span> <span class="ss">:production</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;pg&#39;</span><span class="p">,</span> <span class="s1">&#39;0.12.2&#39;</span>
<span class="k">end</span>
</pre></div>
</div>
<p>Heroku recommends against using different databases in development and production, but for the sample application it won&rsquo;t make any difference, and SQLite is <em>much</em> easier than PostgreSQL to install and configure. Installing and configuring PostgreSQL on your local machine is left as an exercise (<a class="ref" href="static-pages.html#sec-static_pages_exercises">Section&nbsp;3.5</a>).</p>
<p>To install and include the new gems, we run <code>bundle install</code>:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle install --without production
</pre></div>
</div>
<p>As in <a class="ref" href="beginning.html#sec-heroku_setup">Section&nbsp;1.4.1</a> and <a class="ref" href="a-demo-app.html#top">Chapter&nbsp;2</a>, we suppress the installation of production gems using the option <tt class="verb">--without</tt> <tt class="verb">production</tt>. This is a &ldquo;remembered option&rdquo;, which means that we don&rsquo;t have to include it in future invocations of Bundler. Instead, we can write simply <code>bundle install</code> and production gems will be ignored automatically.<sup class="footnote" id="fnref-3_2"><a href="#fn-3_2">2</a></sup></p>
<p>Next, we need to configure Rails to use RSpec in place of <code>Test::Unit</code>. This can be accomplished with <code>rails generate rspec:install</code>:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails generate rspec:install
</pre></div>
</div>
<p>If your system complains about the lack of a JavaScript runtime, visit the <a href="https://github.com/sstephenson/execjs">execjs page at GitHub</a> for a list of possibilities. I particularly recommend installing <a href="http://nodejs.org/">Node.js</a>.</p>
<p>With that, all we have left is to initialize the Git repository:<sup class="footnote" id="fnref-3_3"><a href="#fn-3_3">3</a></sup></p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git init
<span class="gp">$</span> git add .
<span class="gp">$</span> git commit -m <span class="s2">&quot;Initial commit&quot;</span>
</pre></div>
</div>
<p>As with the first application, I suggest updating the <code>README</code> file (located in the root directory of the application) to be more helpful and descriptive, as shown in <a class="ref" href="static-pages.html#code-sample_app_readme">Listing&nbsp;3.2</a>.</p>
<div class="label" id="code-sample_app_readme"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.2.</span> <span class="description">An improved <code>README</code> file for the sample app.</span> </div>
<div class="code"><div class="highlight"><pre># Ruby on Rails Tutorial: sample application
This is the sample application for
[*Ruby on Rails Tutorial: Learn Rails by Example*](http://railstutorial.org/)
by [Michael Hartl](http://michaelhartl.com/).
</pre></div>
</div></div>
<p>Then change it to use the Markdown extension&nbsp;<code>.md</code> and commit the changes:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git mv README.rdoc README.md
<span class="gp">$</span> git commit -a -m <span class="s2">&quot;Improve the README&quot;</span>
</pre></div>
</div>
<div class="label" id="fig-create_repository"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/create_repository_new.png" alt="create_repository_new" /></span></div><div class="caption"><span class="header">Figure 3.1: </span><span class="description">Creating the sample app repository at GitHub.&nbsp;<a href="http://railstutorial.org/images/figures/create_repository_new-full.png">(full size)</a></span></div></div>
<p>Since we&rsquo;ll be using this sample app throughout the rest of the book, it&rsquo;s a good idea to make a repository at GitHub (<a class="ref" href="static-pages.html#fig-create_repository">Figure&nbsp;3.1</a>) and push it up:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git remote add origin git@github.com:&lt;username&gt;/sample_app.git
<span class="gp">$</span> git push -u origin master
</pre></div>
</div>
<p>As a result of my performing this step, you can find the <a href="https://github.com/railstutorial/sample_app_2nd_ed">Rails Tutorial sample application code on GitHub</a> (under a slightly different name).<sup class="footnote" id="fnref-3_4"><a href="#fn-3_4">4</a></sup></p>
<p>Of course, we can optionally deploy the app to Heroku even at this early stage:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> heroku create --stack cedar
<span class="gp">$</span> git push heroku master
</pre></div>
</div>
<p>As you proceed through the rest of the book, I recommend pushing and deploying the application regularly:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git push
<span class="gp">$</span> git push heroku
</pre></div>
</div>
<p>This provides remote backups and lets you catch any production errors as soon as possible. If you run into problems at Heroku, make sure to take a look at the production logs to try to diagnose the problem:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> heroku logs
</pre></div>
</div>
<p>With all the preparation finished, we&rsquo;re finally ready to get started developing the sample application.</p>
<div class="label" id="sec-static_pages"></div>
<h2><a id="sec-3_1" href="static-pages.html#sec-static_pages" class="heading"><span class="number">3.1</span> Static pages</a></h2>
<p>Rails has two main ways of making static web pages. First, Rails can handle <em>truly</em> static pages consisting of raw HTML files. Second, Rails allows us to define <em>views</em> containing raw HTML, which Rails can <em>render</em> so that the web server can send it to the browser.</p>
<p>In order to get our bearings, it&rsquo;s helpful to recall the Rails directory structure from <a class="ref" href="beginning.html#sec-the_first_application">Section&nbsp;1.2.3</a> (<a class="ref" href="beginning.html#fig-directory_structure_rails_3_2">Figure&nbsp;1.2</a>). In this section, we&rsquo;ll be working mainly in the <code>app/controllers</code> and <code>app/views</code> directories. (In <a class="ref" href="static-pages.html#sec-first_tests">Section&nbsp;3.2</a>, we&rsquo;ll even add a new directory of our own.)</p>
<p>This is the first section where it&rsquo;s useful to be able to open the entire Rails directory in your text editor or IDE. Unfortunately, how to do this is system-dependent, but in many cases you can open the current application directory, represented in Unix by a dot&nbsp;<code>.</code>, using the command-line command for your editor of choice:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> <span class="nb">cd</span> ~/rails_projects/sample_app
<span class="gp">$</span> &lt;editor name&gt; .
</pre></div>
</div>
<p>For example, to open the sample app in Sublime Text, you type</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> subl .
</pre></div>
</div>
<p>For Vim, you type <code>vim .</code>, <code>gvim .</code>, or <code>mvim .</code> depending on which flavor of Vim you use.</p>
<div class="label" id="sec-truly_static_pages"></div>
<h3><a id="sec-3_1_1" href="static-pages.html#sec-truly_static_pages" class="heading"><span class="number">3.1.1</span> Truly static pages</a></h3>
<p>We start with truly static pages. Recall from <a class="ref" href="beginning.html#sec-rails_server">Section&nbsp;1.2.5</a> that every Rails application comes with a minimal working application thanks to the <code>rails</code> script, with a default welcome page at the address <a href="http://localhost:3000/">http://localhost:3000/</a> (<a class="ref" href="beginning.html#fig-riding_rails_31">Figure&nbsp;1.3</a>).</p>
<div class="label" id="fig-public_index_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/public_index_rails_3.png" alt="public_index_rails_3" /></span></div><div class="caption"><span class="header">Figure 3.2: </span><span class="description">The <code>public/index.html</code> file.&nbsp;<a href="http://railstutorial.org/images/figures/public_index_rails_3-full.png">(full size)</a></span></div></div>
<p>To learn where this page comes from, take a look at the file <code>public/index.html</code> (<a class="ref" href="static-pages.html#fig-public_index_rails_3">Figure&nbsp;3.2</a>). Because the file contains its own stylesheet information, it&rsquo;s a little messy, but it gets the job done: by default, Rails serves any files in the <code>public</code> directory directly to the browser.<sup class="footnote" id="fnref-3_5"><a href="#fn-3_5">5</a></sup> In the case of the special <code>index.html</code> file, you don&rsquo;t even have to indicate the file in the URI, as <code>index.html</code> is the default. You can include it if you want, though; the addresses http://localhost:3000/ http://localhost:3000/index.html are equivalent.</p>
<p>As you might expect, if we want we can make our own static HTML files and put them in the same <code>public</code> directory as <code>index.html</code>. For example, let&rsquo;s create a file with a friendly greeting (<a class="ref" href="static-pages.html#code-hello_world">Listing&nbsp;3.3</a>):<sup class="footnote" id="fnref-3_6"><a href="#fn-3_6">6</a></sup></p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> subl public/hello.html
</pre></div>
</div>
<div class="label" id="code-hello_world"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.3.</span> <span class="description">A typical HTML file, with a friendly greeting. <br /> <code>public/hello.html</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;title&gt;</span>Greeting<span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
<span class="nt">&lt;p&gt;</span>Hello, world!<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div></div>
<p>We see in <a class="ref" href="static-pages.html#code-hello_world">Listing&nbsp;3.3</a> the typical structure of an HTML document: a <em>document type</em>, or doctype, declaration at the top to tell browsers which version of HTML we&rsquo;re using (in this case, <a href="http://en.wikipedia.org/wiki/HTML5">HTML5</a>);<sup class="footnote" id="fnref-3_7"><a href="#fn-3_7">7</a></sup> a <code>head</code> section, in this case with &ldquo;Greeting&rdquo; inside a <code>title</code> tag; and a <code>body</code> section, in this case with &ldquo;Hello, world!&rdquo; inside a <code>p</code> (paragraph) tag. (The indentation is optional&mdash;HTML is not sensitive to whitespace, and ignores both tabs and spaces&mdash;but it makes the document&rsquo;s structure easier to see.)</p>
<p>Now run a local server using</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails server
</pre></div>
</div>
<p>and navigate to <a href="http://localhost:3000/hello.html">http://localhost:3000/hello.html</a>. As promised, Rails renders the page straightaway (<a class="ref" href="static-pages.html#fig-hello_world">Figure&nbsp;3.3</a>). Note that the title displayed at the top of the browser window in <a class="ref" href="static-pages.html#fig-hello_world">Figure&nbsp;3.3</a> is just the contents inside the <code>title</code> tag, namely, &ldquo;Greeting&rdquo;.</p>
<div class="label" id="fig-hello_world"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/hello_world.png" alt="hello_world" /></span></div><div class="caption"><span class="header">Figure 3.3: </span><span class="description">A new static HTML file.&nbsp;<a href="http://railstutorial.org/images/figures/hello_world-full.png">(full size)</a></span></div></div>
<p>Since this file is just for demonstration purposes, we don&rsquo;t really want it to be part of our sample application, so it&rsquo;s probably best to remove it once the thrill of creating it has worn off:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rm public/hello.html
</pre></div>
</div>
<p>We&rsquo;ll leave the <code>index.html</code> file alone for now, but of course eventually we should remove it: we don&rsquo;t want the root of our application to be the Rails default page shown in <a class="ref" href="beginning.html#fig-riding_rails_31">Figure&nbsp;1.3</a>. We&rsquo;ll see in <a class="ref" href="filling-in-the-layout.html#sec-layout_links">Section&nbsp;5.3</a> how to change the address <a href="http://localhost:3000/">http://localhost:3000/</a> to point to something other than <code>public/index.html</code>.</p>
<div class="label" id="sec-static_pages_with_rails"></div>
<h3><a id="sec-3_1_2" href="static-pages.html#sec-static_pages_with_rails" class="heading"><span class="number">3.1.2</span> Static pages with Rails</a></h3>
<p>The ability to return static HTML files is nice, but it&rsquo;s not particularly useful for making dynamic web applications. In this section, we&rsquo;ll take a first step toward making dynamic pages by creating a set of Rails <em>actions</em>, which are a more powerful way to define URIs than static files.<sup class="footnote" id="fnref-3_8"><a href="#fn-3_8">8</a></sup> Rails actions come bundled together inside <em>controllers</em> (the C in MVC from <a class="ref" href="beginning.html#sec-mvc">Section&nbsp;1.2.6</a>), which contain sets of actions related by a common purpose. We got a glimpse of controllers in <a class="ref" href="a-demo-app.html#top">Chapter&nbsp;2</a>, and will come to a deeper understanding once we explore the <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">REST architecture</a> more fully (starting in <a class="ref" href="modeling-users.html#top">Chapter&nbsp;6</a>); in essence, a controller is a container for a group of (possibly dynamic) web pages.</p>
<p>To get started, recall from <a class="ref" href="beginning.html#sec-git_commands">Section&nbsp;1.3.5</a> that, when using Git, it&rsquo;s a good practice to do our work on a separate topic branch rather than the master branch. If you&rsquo;re using Git for version control, you should run the following command:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git checkout -b static-pages
</pre></div>
</div>
<p>Rails comes with a script for making controllers called <code>generate</code>; all it needs to work its magic is the controller&rsquo;s name. In order to use <code>generate</code> with RSpec, you need to run the RSpec generator command if you didn&rsquo;t run it when following the introduction to this chapter:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails generate rspec:install
</pre></div>
</div>
<p>Since we&rsquo;ll be making a controller to handle static pages, we&rsquo;ll call it the StaticPages controller. We&rsquo;ll also plan to make actions for a Home page, a Help page, and an About page. The <code>generate</code> script takes an optional list of actions, so we&rsquo;ll include two of the initial actions directly on the command line (<a class="ref" href="static-pages.html#code-generating_pages">Listing&nbsp;3.4</a>).</p>
<div class="label" id="code-generating_pages"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.4.</span> <span class="description">Generating a StaticPages controller.</span> </div>
<div class="code"><div class="highlight"><pre>$ rails generate controller StaticPages home help --no-test-framework
create app/controllers/static_pages_controller.rb
route get &quot;static_pages/help&quot;
route get &quot;static_pages/home&quot;
invoke erb
create app/views/static_pages
create app/views/static_pages/home.html.erb
create app/views/static_pages/help.html.erb
invoke helper
create app/helpers/static_pages_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/static_pages.js.coffee
invoke scss
create app/assets/stylesheets/static_pages.css.scss
</pre></div>
</div></div>
<p>Here we&rsquo;ve used the option <tt class="verb">--no-test-framework</tt> to suppress the generation of the default RSpec tests, which we won&rsquo;t be using. Instead, we&rsquo;ll create the tests by hand starting in <a class="ref" href="static-pages.html#sec-first_tests">Section&nbsp;3.2</a>. We&rsquo;ve also intentionally left off the <code>about</code> action from the command line arguments in <a class="ref" href="static-pages.html#code-generating_pages">Listing&nbsp;3.4</a> so that we can see how to add it using test-driven development, or TDD (<a class="ref" href="static-pages.html#sec-first_tests">Section&nbsp;3.2</a>).</p>
<p>In <a class="ref" href="static-pages.html#code-generating_pages">Listing&nbsp;3.4</a>, note that we have passed the controller name as so-called <a href="https://en.wikipedia.org/wiki/CamelCase">CamelCase</a>, which leads to the creation of a controller file written in <a href="https://en.wikipedia.org/wiki/Snake_case">snake case</a>, so that a controller called StaticPages yields a file called static_pages_controller.rb. This is merely a convention, and in fact using snake case at the command line also works: the command</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails generate controller static_pages ...
</pre></div>
</div>
<p>also generates a controller called static_pages_controller.rb. Because Ruby uses CamelCase for class names (<a class="ref" href="rails-flavored-ruby.html#sec-ruby_classes">Section&nbsp;4.4</a>), my preference is to refer to controllers using their CamelCase names, but this is a matter of taste. (Since Ruby filenames typically use snake case, the Rails generator converts CamelCase to snake case using the <a href="http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore"><tt>underscore</tt></a> method.)</p>
<p>By the way, if you ever make a mistake when generating code, it&rsquo;s useful to know how to reverse the process. See <a class="ref" href="static-pages.html#sidebar-undoing_things">Box&nbsp;3.1</a> for some techniques on how to undo things in Rails.</p>
<div class="label" id="sidebar-undoing_things"></div>
<div class="sidebar"><span class="title"><span class="header">Box 3.1.</span><span class="description">Undoing things</span></span>
<p>Even when you&rsquo;re very careful, things can sometimes go wrong when developing Rails applications. Happily, Rails has some facilities to help you recover.</p>
<p>One common scenario is wanting to undo code generation&mdash;for example, if you change your mind on the name of a controller. When generating a controller, Rails creates many more files than the controller file itself (as seen in <a class="ref" href="static-pages.html#code-generating_pages">Listing&nbsp;3.4</a>). Undoing the generation means removing not only the principal generated file, but all the ancillary files as well. (In fact, we also want to undo any automatic edits made to the <tt>routes.rb</tt> file.) In Rails, this can be accomplished with <tt>rails destroy</tt>. In particular, these two commands cancel each other out:</p>
<pre class="verbatim"> $ rails generate controller FooBars baz quux
$ rails destroy controller FooBars baz quux</pre>
<p>Similarly, in <a class="ref" href="modeling-users.html#top">Chapter&nbsp;6</a> we&rsquo;ll generate a <em>model</em> as follows:</p>
<pre class="verbatim"> $ rails generate model Foo bar:string baz:integer</pre>
<p>This can be undone using</p>
<pre class="verbatim"> $ rails destroy model Foo</pre>
<p>(In this case, it turns out we can omit the other command-line arguments. When you get to <a class="ref" href="modeling-users.html#top">Chapter&nbsp;6</a>, see if you can figure out why.)</p>
<p>Another technique related to models involves undoing <em>migrations</em>, which we saw briefly in <a class="ref" href="a-demo-app.html#top">Chapter&nbsp;2</a> and will see much more of starting in <a class="ref" href="modeling-users.html#top">Chapter&nbsp;6</a>. Migrations change the state of the database using</p>
<pre class="verbatim"> $ rake db:migrate</pre>
<p>We can undo a single migration step using</p>
<pre class="verbatim"> $ rake db:rollback</pre>
<p>To go all the way back to the beginning, we can use</p>
<pre class="verbatim"> $ rake db:migrate VERSION=0</pre>
<p>As you might guess, substituting any other number for <tt>0</tt> migrates to that version number, where the version numbers come from listing the migrations sequentially.</p>
<p>With these techniques in hand, we are well-equipped to recover from the inevitable development <a href="http://en.wikipedia.org/wiki/SNAFU">snafus</a>.</p>
</div>
<p>The StaticPages controller generation in <a class="ref" href="static-pages.html#code-generating_pages">Listing&nbsp;3.4</a> automatically updates the <em>routes</em> file, called <code>config/routes.rb</code>, which Rails uses to find the correspondence between URIs and web pages. This is our first encounter with the <code>config</code> directory, so it&rsquo;s helpful to take a quick look at it (<a class="ref" href="static-pages.html#fig-config_directory_rails_3">Figure&nbsp;3.4</a>). The <code>config</code> directory is where Rails collects files needed for the application configuration&mdash;hence the name.</p>
<div class="label" id="fig-config_directory_rails_3"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/config_directory_rails_3.png" alt="config_directory_rails_3" /></span></div><div class="caption"><span class="header">Figure 3.4: </span><span class="description">Contents of the sample app&rsquo;s <code>config</code> directory.&nbsp;<a href="http://railstutorial.org/images/figures/config_directory_rails_3-full.png">(full size)</a></span></div></div>
<p>Since we generated <code>home</code> and <code>help</code> actions, the routes file already has a rule for each one, as seen in <a class="ref" href="static-pages.html#code-pages_routes">Listing&nbsp;3.5</a>.</p>
<div class="label" id="code-pages_routes"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.5.</span> <span class="description">The routes for the <code>home</code> and <code>help</code> actions in the StaticPages controller. <br /> <code>config/routes.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="ss">SampleApp::Application</span><span class="o">.</span><span class="n">routes</span><span class="o">.</span><span class="n">draw</span> <span class="k">do</span>
<span class="n">get</span> <span class="s2">&quot;static_pages/home&quot;</span>
<span class="n">get</span> <span class="s2">&quot;static_pages/help&quot;</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Here the rule</p>
<div class="code"><div class="highlight"><pre><span class="n">get</span> <span class="s2">&quot;static_pages/home&quot;</span>
</pre></div>
</div>
<p>maps requests for the URI /static_pages/home to the <code>home</code> action in the StaticPages controller. Moreover, by using <code>get</code> we arrange for the route to respond to a <tt>GET</tt> request, which is one of the fundamental
<em>HTTP verbs</em> supported by the hypertext transfer protocol (<a class="ref" href="static-pages.html#sidebar-get_etc">Box&nbsp;3.2</a>). In our case, this means that when we generate a <code>home</code> action inside the StaticPages controller we automatically get a page at the address /static_pages/home. To see the results, navigate to <a href="http://localhost:3000/static_pages/home">/static_pages/home</a> (<a class="ref" href="static-pages.html#fig-raw_home_view">Figure&nbsp;3.5</a>).</p>
<div class="label" id="fig-raw_home_view"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/raw_home_view_31.png" alt="raw_home_view_31" /></span></div><div class="caption"><span class="header">Figure 3.5: </span><span class="description">The raw home view (<a href="http://localhost:3000/static_pages/home">/static_pages/home</a>).&nbsp;<a href="http://railstutorial.org/images/figures/raw_home_view_31-full.png">(full size)</a></span></div></div>
<div class="label" id="sidebar-get_etc"></div>
<div class="sidebar"><span class="title"><span class="header">Box 3.2.</span><span class="description"><tt>GET</tt>, et cet.</span></span>
<p>The hypertext transfer protocol (<a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods">HTTP</a>) defines four basic operations, corresponding to the four verbs <em>get</em>, <em>post</em>, <em>put</em>, and <em>delete</em>. These refer to operations between a <em>client</em> computer (typically running a web browser such as Firefox or Safari) and a <em>server</em> (typically running a web server such as Apache or Nginx). (It&rsquo;s important to understand that, when developing Rails applications on a local computer, the client and server are the same physical machine, but in general they are different.) An emphasis on HTTP verbs is typical of web frameworks (including Rails) influenced by the <em>REST architecture</em>, which we saw briefly in <a class="ref" href="a-demo-app.html#top">Chapter&nbsp;2</a> and will start learning about more in <a class="ref" href="sign-up.html#top">Chapter&nbsp;7</a>.</p>
<p><tt>GET</tt> is the most common HTTP operation, used for <em>reading</em> data on the web; it just means &ldquo;get a page&rdquo;, and every time you visit a site like google.com or wikipedia.org your browser is submitting a <tt>GET</tt> request. <tt>POST</tt> is the next most common operation; it is the request sent by your browser when you submit a form. In Rails applications, <tt>POST</tt> requests are typically used for <em>creating</em> things (although HTTP also allows <tt>POST</tt> to perform updates); for example, the <tt>POST</tt> request sent when you submit a registration form creates a new user on the remote site. The other two verbs, <tt>PUT</tt> and <tt>DELETE</tt>, are designed for <em>updating</em> and <em>destroying</em> things on the remote server. These requests are less common than <tt>GET</tt> and <tt>POST</tt> since browsers are incapable of sending them natively, but some web frameworks (including Ruby on Rails) have clever ways of making it <em>seem</em> like browsers are issuing such requests.</p>
</div>
<p>To understand where this page comes from, let&rsquo;s start by taking a look at the StaticPages controller in a text editor; you should see something like <a class="ref" href="static-pages.html#code-static_pages_controller">Listing&nbsp;3.6</a>. You may note that, unlike the demo Users and Microposts controllers from <a class="ref" href="a-demo-app.html#top">Chapter&nbsp;2</a>, the StaticPages controller does not use the standard REST actions. This is normal for a collection of static pages&mdash;the REST architecture isn&rsquo;t the best solution to every problem.</p>
<div class="label" id="code-static_pages_controller"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.6.</span> <span class="description">The StaticPages controller made by <a class="ref" href="static-pages.html#code-generating_pages">Listing&nbsp;3.4</a>. <br /> <code>app/controllers/static_pages_controller.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">StaticPagesController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
<span class="k">def</span> <span class="nf">home</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">help</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>We see from the <code>class</code> keyword in <a class="ref" href="static-pages.html#code-static_pages_controller">Listing&nbsp;3.6</a> that <code>static_pages_controller.rb</code> defines a <em>class</em>, in this case called <code>StaticPagesController</code>. Classes are simply a convenient way to organize <em>functions</em> (also called <em>methods</em>) like the <code>home</code> and <code>help</code> actions, which are defined using the <code>def</code> keyword. The angle bracket&nbsp;<code>&lt;</code> indicates that <code>StaticPagesController</code> <em>inherits</em> from the Rails class <code>ApplicationController</code>; as we&rsquo;ll see momentarily, this means that our pages come equipped with a large amount of Rails-specific functionality. (We&rsquo;ll learn more about both classes and inheritance in <a class="ref" href="rails-flavored-ruby.html#sec-ruby_classes">Section&nbsp;4.4</a>.)</p>
<p>In the case of the StaticPages controller, both its methods are initially empty:</p>
<div class="code"><div class="highlight"><pre><span class="k">def</span> <span class="nf">home</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">help</span>
<span class="k">end</span>
</pre></div>
</div>
<p>In plain Ruby, these methods would simply do nothing. In Rails, the situation is different; <code>StaticPagesController</code> is a Ruby class, but because it inherits from <code>ApplicationController</code> the behavior of its methods is specific to Rails: when visiting the URI /static_pages/home, Rails looks in the StaticPages controller and executes the code in the <code>home</code> action, and then renders the <em>view</em> (the V in MVC from <a class="ref" href="beginning.html#sec-mvc">Section&nbsp;1.2.6</a>) corresponding to the action. In the present case, the <code>home</code> action is empty, so all visiting /static_pages/home does is render the view. So, what does a view look like, and how do we find it?</p>
<p>If you take another look at the output in <a class="ref" href="static-pages.html#code-generating_pages">Listing&nbsp;3.4</a>, you might be able to guess the correspondence between actions and views: an action like <code>home</code> has a corresponding view called <code>home.html.erb</code>. We&rsquo;ll learn in <a class="ref" href="static-pages.html#sec-slightly_dynamic_pages">Section&nbsp;3.3</a> what the <code>.erb</code> part means; from the <code>.html</code> part you probably won&rsquo;t be surprised that it basically looks like HTML (<a class="ref" href="static-pages.html#code-raw_home_view">Listing&nbsp;3.7</a>).</p>
<div class="label" id="code-raw_home_view"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.7.</span> <span class="description">The generated view for the Home page. <br /> <code>app/views/static_pages/home.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nt">&lt;h1&gt;</span>StaticPages#home<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>Find me in app/views/static_pages/home.html.erb<span class="nt">&lt;/p&gt;</span>
</pre></div>
</div></div>
<p>The view for the <code>help</code> action is analogous (<a class="ref" href="static-pages.html#code-raw_help_view">Listing&nbsp;3.8</a>).</p>
<div class="label" id="code-raw_help_view"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.8.</span> <span class="description">The generated view for the Help page. <br /> <code>app/views/static_pages/help.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nt">&lt;h1&gt;</span>StaticPages#help<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>Find me in app/views/static_pages/help.html.erb<span class="nt">&lt;/p&gt;</span>
</pre></div>
</div></div>
<p>Both of these views are just placeholders: they have a top-level heading (inside the <code>h1</code> tag) and a paragraph (<code>p</code> tag) with the full path to the relevant file. We&rsquo;ll add some (very slightly) dynamic content starting in <a class="ref" href="static-pages.html#sec-slightly_dynamic_pages">Section&nbsp;3.3</a>, but as they stand these views underscore an important point: Rails views can simply contain static HTML. As far as the browser is concerned, the raw HTML files from <a class="ref" href="static-pages.html#sec-truly_static_pages">Section&nbsp;3.1.1</a> and the controller/action method of delivering pages are indistinguishable: all the browser ever sees is HTML.</p>
<p>In the remainder of this chapter, we&rsquo;ll add some custom content to the Home and Help pages, and then add in the About page we left off in <a class="ref" href="static-pages.html#sec-static_pages_with_rails">Section&nbsp;3.1.2</a>. Then we&rsquo;ll add a very small amount of dynamic content by changing the title on a per-page basis.</p>
<p>Before moving on, if you&rsquo;re using Git it&rsquo;s a good idea to add the files for the StaticPages controller to the repository:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git add .
<span class="gp">$</span> git commit -m <span class="s2">&quot;Add a StaticPages controller&quot;</span>
</pre></div>
</div>
<div class="label" id="sec-first_tests"></div>
<h2><a id="sec-3_2" href="static-pages.html#sec-first_tests" class="heading"><span class="number">3.2</span> Our first tests</a></h2>
<p>The <em>Rails Tutorial</em> takes an intuitive approach to testing that emphasizes the behavior of the application rather than its precise implementation, a variant of test-driven development (TDD) known as behavior-driven development (BDD). Our main tools will be <em>integration tests</em> (starting in this section) and <em>unit tests</em> (starting in <a class="ref" href="modeling-users.html#top">Chapter&nbsp;6</a>). Integration tests, known as <em>request specs</em> in the context of RSpec, allow us to simulate the actions of a user interacting with our application using a web browser. Together with the natural-language syntax provided by Capybara, integration tests provide a powerful method to test our application&rsquo;s functionality without having to manually check each page with a browser. (Another popular choice for BDD, called Cucumber, is introduced in <a class="ref" href="sign-in-sign-out.html#sec-cucumber">Section&nbsp;8.3</a>.)</p>
<p>The defining quality of TDD is writing tests <em>first</em>, before the application code. Initially, this might take some getting used to, but the benefits are significant. By writing a <em>failing</em> test first and then implementing the application code to get it to pass, we increase our confidence that the test is actually covering the functionality we think it is. Moreover, the fail-implement-pass development cycle induces a <a href="http://en.wikipedia.org/wiki/Flow_(psychology)">flow state</a>, leading to enjoyable coding and high productivity. Finally, the tests act as a <em>client</em> for the application code, often leading to more elegant software designs.</p>
<p>It&rsquo;s important to understand that TDD is not always the right tool for the job: there&rsquo;s no reason to dogmatically insist that tests always should be written first, that they should cover every single feature, or that there should necessarily be any tests at all. For example, when you aren&rsquo;t at all sure how to solve a given programming problem, it&rsquo;s often useful to skip the tests and write only application code, just to get a sense of what the solution will look like. (In the language of <a href="http://en.wikipedia.org/wiki/Extreme_Programming">Extreme Programming (XP)</a>, this exploratory step is called a <em>spike</em>.) Once you see the general shape of the solution, you can then use TDD to implement a more polished version.</p>
<p>In this section, we&rsquo;ll be running the tests using the <code>rspec</code> command supplied by the RSpec gem. This practice is straightforward but not ideal, and if you are a more advanced user I suggest setting up your system as described in <a class="ref" href="static-pages.html#sec-advanced_setup">Section&nbsp;3.6</a>.</p>
<div class="label" id="sec-TDD"></div>
<h3><a id="sec-3_2_1" href="static-pages.html#sec-TDD" class="heading"><span class="number">3.2.1</span> Test-driven development</a></h3>
<p>In test-driven development, we first write a <em>failing</em> test, represented in many testing tools by the color red. We then implement code to get the test to pass, represented by the color green. Finally, if necessary, we refactor the code, changing its form (by eliminating duplication, for example) without changing its function. This cycle is known as &ldquo;Red, Green, Refactor&rdquo;.</p>
<p>We&rsquo;ll begin by adding some content to the Home page using test-driven development, including a top-level heading (<code>&lt;h1&gt;</code>) with the content <code>Sample App</code>. The first step is to generate an integration test (request spec) for our static pages:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rails generate integration_test static_pages
<span class="go"> invoke rspec</span>
<span class="go"> create spec/requests/static_pages_spec.rb</span>
</pre></div>
</div>
<p>This creates the <code>static_pages_spec.rb</code> in the <code>spec/requests</code> directory. As with most generated code, the result is not pretty, so let&rsquo;s open <code>static_pages_spec.rb</code> with a text editor and replace it with the contents of <a class="ref" href="static-pages.html#code-home_page_content_spec">Listing&nbsp;3.9</a>.</p>
<div class="label" id="code-home_page_content_spec"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.9.</span> <span class="description">Code to test the contents of the Home page. <br /> <code>spec/requests/static_pages_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">&#39;spec_helper&#39;</span>
<span class="n">describe</span> <span class="s2">&quot;Static pages&quot;</span> <span class="k">do</span>
<span class="n">describe</span> <span class="s2">&quot;Home page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the content &#39;Sample App&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/home&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">&#39;Sample App&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>The code in <a class="ref" href="static-pages.html#code-home_page_content_spec">Listing&nbsp;3.9</a> is pure Ruby, but even if you&rsquo;ve studied Ruby before it probably won&rsquo;t look very familiar. This is because RSpec uses the general malleability of Ruby to define a <em>domain-specific language</em> (DSL) built just for testing. The important point is that <em>you do not need to understand RSpec&rsquo;s syntax to be able to use RSpec</em>. It may seem like magic at first, but RSpec and Capybara are designed to read more or less like English, and if you follow the examples from the <code>generate</code> script and the other examples in this tutorial you&rsquo;ll pick it up fairly quickly.</p>
<p><a class="ref" href="static-pages.html#code-home_page_content_spec">Listing&nbsp;3.9</a> contains a <code>describe</code> block with one <em>example</em>, i.e., a block starting with <code>it "&hellip;" do</code>:</p>
<div class="code"><div class="highlight"><pre><span class="n">describe</span> <span class="s2">&quot;Home page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the content &#39;Sample App&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/home&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">&#39;Sample App&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div>
<p>The first line indicates that we are describing the Home page. This is just a string, and it can be anything you want; RSpec doesn&rsquo;t care, but you and other human readers probably do. Then the spec says that when you visit the Home page at <code>/static_pages/home</code>, the content should contain the words &ldquo;Sample App&rdquo;. As with the first line, what goes inside the quote marks is irrelevant to RSpec, and is intended to be descriptive to human readers. Then the line</p>
<div class="code"><div class="highlight"><pre><span class="n">visit</span> <span class="s1">&#39;/static_pages/home&#39;</span>
</pre></div>
</div>
<p>uses the Capybara function <code>visit</code> to simulate visiting the URI <code>/static_pages/home</code> in a browser, while the line</p>
<div class="code"><div class="highlight"><pre><span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">&#39;Sample App&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>uses the <code>page</code> variable (also provided by Capybara) to test that the resulting page has the right content.</p>
<p>To run the test, we have several options, including some convenient but rather advanced tools discussed in <a class="ref" href="static-pages.html#sec-advanced_setup">Section&nbsp;3.6</a>. For now, we&rsquo;ll use the <code>rspec</code> command at the command line (executed with <code>bundle exec</code> to ensure that RSpec runs in the environment specified by our <code>Gemfile</code>):<sup class="footnote" id="fnref-3_9"><a href="#fn-3_9">9</a></sup></p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>This yields a failing test. The appearance of the result depends on your system; on my system, the red failing test appears as in <a class="ref" href="static-pages.html#fig-red_failing_spec">Figure&nbsp;3.6</a>.<sup class="footnote" id="fnref-3_10"><a href="#fn-3_10">10</a></sup> (The screenshot, which predates, the current Git branching strategy, shows work on the <tt>master</tt> branch instead the <tt>static-pages</tt> branch, but this is not cause for concern.)</p>
<div class="label" id="fig-red_failing_spec"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/red_failing_spec.png" alt="red_failing_spec" /></span></div><div class="caption"><span class="header">Figure 3.6: </span><span class="description">A red (failing) test.&nbsp;<a href="http://railstutorial.org/images/figures/red_failing_spec-full.png">(full size)</a></span></div></div>
<p>To get the test to pass, we&rsquo;ll replace the default Home page test with the HTML in <a class="ref" href="static-pages.html#code-home_page_passing">Listing&nbsp;3.10</a>.</p>
<div class="label" id="code-home_page_passing"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.10.</span> <span class="description">Code to get a passing test for the Home page. <br /> <code>app/views/static_pages/home.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nt">&lt;h1&gt;</span>Sample App<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
This is the home page for the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails Tutorial<span class="nt">&lt;/a&gt;</span>
sample application.
<span class="nt">&lt;/p&gt;</span>
</pre></div>
</div></div>
<p>This arranges for a top-level heading (<code>&lt;h1&gt;</code>) with the content <code>Sample App</code>, which should get the test to pass. We also include an <em>anchor</em> tag&nbsp;<code>a</code>, which creates links to the given URI (called an &ldquo;href&rdquo;, or &ldquo;hypertext reference&rdquo;, in the context of an anchor tag):</p>
<div class="code"><div class="highlight"><pre><span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails Tutorial<span class="nt">&lt;/a&gt;</span>
</pre></div>
</div>
<p>Now re-run the test to see the effect:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>On my system, the passing test appears as in <a class="ref" href="static-pages.html#fig-green_passing_spec">Figure&nbsp;3.7</a>.</p>
<div class="label" id="fig-green_passing_spec"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/green_passing_spec.png" alt="green_passing_spec" /></span></div><div class="caption"><span class="header">Figure 3.7: </span><span class="description">A green (passing) test.&nbsp;<a href="http://railstutorial.org/images/figures/green_passing_spec-full.png">(full size)</a></span></div></div>
<p>Based on the example for the Home page, you can probably guess the analogous test and application code for the Help page. We start by testing for the relevant content, in this case the string <code>&rsquo;Help&rsquo;</code> (<a class="ref" href="static-pages.html#code-help_content_spec">Listing&nbsp;3.11</a>).</p>
<div class="label" id="code-help_content_spec"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.11.</span> <span class="description">Adding code to test the contents of the Help page. <br /> <code>spec/requests/static_pages_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">&#39;spec_helper&#39;</span>
<span class="n">describe</span> <span class="s2">&quot;Static pages&quot;</span> <span class="k">do</span>
<span class="n">describe</span> <span class="s2">&quot;Home page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the content &#39;Sample App&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/home&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">&#39;Sample App&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">&quot;Help page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the content &#39;Help&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/help&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">&#39;Help&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Then run the tests:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>One test should fail. (Since systems will vary, and since keeping track of how many tests there are at each stage of the tutorial is a maintenance nightmare, I&rsquo;ll omit the RSpec output from now on.)</p>
<p>The application code (which for now is raw HTML) is similar to the code in <a class="ref" href="static-pages.html#code-home_page_passing">Listing&nbsp;3.10</a>, as seen in <a class="ref" href="static-pages.html#code-help_page_passing">Listing&nbsp;3.12</a>.</p>
<div class="label" id="code-help_page_passing"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.12.</span> <span class="description">Code to get a passing test for the Help page. <br /> <code>app/views/static_pages/help.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nt">&lt;h1&gt;</span>Help<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
Get help on the Ruby on Rails Tutorial at the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/help&quot;</span><span class="nt">&gt;</span>Rails Tutorial help page<span class="nt">&lt;/a&gt;</span>.
To get help on this sample app, see the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/book&quot;</span><span class="nt">&gt;</span>Rails Tutorial book<span class="nt">&lt;/a&gt;</span>.
<span class="nt">&lt;/p&gt;</span>
</pre></div>
</div></div>
<p>The tests should now pass:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<div class="label" id="sec-adding_a_page"></div>
<h3><a id="sec-3_2_2" href="static-pages.html#sec-adding_a_page" class="heading"><span class="number">3.2.2</span> Adding a page</a></h3>
<p>Having seen test-driven development in action in a simple example, we&rsquo;ll use the same technique to accomplish the slightly more complicated task of adding a new page, namely, the About page that we intentionally left off in <a class="ref" href="static-pages.html#sec-static_pages_with_rails">Section&nbsp;3.1.2</a>. By writing a test and running RSpec at each step, we&rsquo;ll see how TDD can guide us through the development of our application code.</p>
<div class="label" id="sec-red"></div>
<h4><a id="sec-3_2_2_1" href="static-pages.html#sec-red" class="heading">Red</a></h4>
<p>We&rsquo;ll get to the Red part of the Red-Green cycle by writing a failing test for the About page. Following the models from <a class="ref" href="static-pages.html#code-help_content_spec">Listing&nbsp;3.11</a>, you can probably guess the right test (<a class="ref" href="static-pages.html#code-about_page_content_spec">Listing&nbsp;3.13</a>).</p>
<div class="label" id="code-about_page_content_spec"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.13.</span> <span class="description">Adding code to test the contents of the About page. <br /> <code>spec/requests/static_pages_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">&#39;spec_helper&#39;</span>
<span class="n">describe</span> <span class="s2">&quot;Static pages&quot;</span> <span class="k">do</span>
<span class="n">describe</span> <span class="s2">&quot;Home page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the content &#39;Sample App&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/home&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">&#39;Sample App&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">&quot;Help page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the content &#39;Help&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/help&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">&#39;Help&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">&quot;About page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the content &#39;About Us&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/about&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_content</span><span class="p">(</span><span class="s1">&#39;About Us&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="sec-green"></div>
<h4><a id="sec-3_2_2_2" href="static-pages.html#sec-green" class="heading">Green</a></h4>
<p>Recall from <a class="ref" href="static-pages.html#sec-static_pages_with_rails">Section&nbsp;3.1.2</a> that we can generate a static page in Rails by creating an action and corresponding view with the page&rsquo;s name. In our case, the About page will first need an action called <code>about</code> in the StaticPages controller. Having written a failing test, we can now be confident that, in getting it to pass, we will actually have created a working About page.</p>
<p>If you run the RSpec example using</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>the output includes the following complaint:</p>
<div class="code"><div class="highlight"><pre>No route matches [GET] &quot;/static_pages/about&quot;
</pre></div>
</div>
<p>This is a hint that we need to add <code>/static_pages/about</code> to the routes file, which we can accomplish by following the pattern in <a class="ref" href="static-pages.html#code-pages_routes">Listing&nbsp;3.5</a>, as shown in <a class="ref" href="static-pages.html#code-about_route">Listing&nbsp;3.14</a>.</p>
<div class="label" id="code-about_route"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.14.</span> <span class="description">Adding the <code>about</code> route. <br /> <code>config/routes.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="ss">SampleApp::Application</span><span class="o">.</span><span class="n">routes</span><span class="o">.</span><span class="n">draw</span> <span class="k">do</span>
<span class="n">get</span> <span class="s2">&quot;static_pages/home&quot;</span>
<span class="n">get</span> <span class="s2">&quot;static_pages/help&quot;</span>
<span class="n">get</span> <span class="s2">&quot;static_pages/about&quot;</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Now running</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>complains that</p>
<div class="code"><div class="highlight"><pre>The action &#39;about&#39; could not be found for StaticPagesController
</pre></div>
</div>
<p>To solve this problem, we follow the model provided by <code>home</code> and <code>help</code> from <a class="ref" href="static-pages.html#code-static_pages_controller">Listing&nbsp;3.6</a> by adding an <code>about</code> action in the StaticPages controller (<a class="ref" href="static-pages.html#code-adding_the_about_page">Listing&nbsp;3.15</a>).</p>
<div class="label" id="code-adding_the_about_page"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.15.</span> <span class="description">The StaticPages controller with added <code>about</code> action. <br /> <code>app/controllers/static_pages_controller.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">StaticPagesController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
<span class="k">def</span> <span class="nf">home</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">help</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">about</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Now running</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>says that we are missing a &ldquo;template&rdquo;, i.e., a view:</p>
<div class="code"><div class="highlight"><pre>ActionView::MissingTemplate:
Missing template static_pages/about
</pre></div>
</div>
<p>To solve this issue, we add the <code>about</code> view. This involves creating a new file called <code>about.html.erb</code> in the <code>app/views/static_pages</code> directory with the contents shown in <a class="ref" href="static-pages.html#code-about_view">Listing&nbsp;3.16</a>.</p>
<div class="label" id="code-about_view"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.16.</span> <span class="description">Code for the About page. <br /> <code>app/views/static_pages/about.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nt">&lt;h1&gt;</span>About Us<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
The <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails Tutorial<span class="nt">&lt;/a&gt;</span>
is a project to make a book and screencasts to teach web development
with <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://rubyonrails.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails<span class="nt">&lt;/a&gt;</span>. This
is the sample application for the tutorial.
<span class="nt">&lt;/p&gt;</span>
</pre></div>
</div></div>
<p>Running RSpec should now get us back to Green:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>Of course, it&rsquo;s never a bad idea to take a look at the page in a browser to make sure our tests aren&rsquo;t completely crazy (<a class="ref" href="static-pages.html#fig-about_us">Figure&nbsp;3.8</a>).</p>
<div class="label" id="fig-about_us"></div>
<div class="figure"><div class="center"><span class="graphic"><img src="images/figures/about_us_2nd_edition.png" alt="about_us_2nd_edition" /></span></div><div class="caption"><span class="header">Figure 3.8: </span><span class="description">The new About page (<a href="http://localhost:3000/static_pages/about">/static_pages/about</a>).&nbsp;<a href="http://railstutorial.org/images/figures/about_us_2nd_edition-full.png">(full size)</a></span></div></div>
<div class="label" id="sec-refactor"></div>
<h4><a id="sec-3_2_2_3" href="static-pages.html#sec-refactor" class="heading">Refactor</a></h4>
<p>Now that we&rsquo;ve gotten to Green, we are free to refactor our code with confidence. Oftentimes code will start to &ldquo;smell&rdquo;, meaning that it gets ugly, bloated, or filled with repetition. The computer doesn&rsquo;t care, of course, but humans do, so it is important to keep the code base clean by refactoring frequently. Having a good test suite is an invaluable tool in this regard, as it dramatically lowers the probability of introducing bugs while refactoring.</p>
<p>Our sample app is a little too small to refactor right now, but code smell seeps in at every crack, so we won&rsquo;t have to wait long: we&rsquo;ll already get busy refactoring in <a class="ref" href="static-pages.html#sec-layouts">Section&nbsp;3.3.4</a>.</p>
<div class="label" id="sec-slightly_dynamic_pages"></div>
<h2><a id="sec-3_3" href="static-pages.html#sec-slightly_dynamic_pages" class="heading"><span class="number">3.3</span> Slightly dynamic pages</a></h2>
<p>Now that we&rsquo;ve created the actions and views for some static pages, we&rsquo;ll make them <em>very slightly</em> dynamic by adding some content that changes on a per-page basis: we&rsquo;ll have the title of each page change to reflect its content. Whether a changing title represents <em>truly</em> dynamic content is debatable, but in any case it lays the necessary foundation for unambiguously dynamic content in <a class="ref" href="sign-up.html#top">Chapter&nbsp;7</a>.</p>
<p>If you skipped the TDD material in <a class="ref" href="static-pages.html#sec-first_tests">Section&nbsp;3.2</a>, be sure to create an About page at this point using the code from <a class="ref" href="static-pages.html#code-about_route">Listing&nbsp;3.14</a>, <a class="ref" href="static-pages.html#code-adding_the_about_page">Listing&nbsp;3.15</a>, and <a class="ref" href="static-pages.html#code-about_view">Listing&nbsp;3.16</a>.</p>
<div class="label" id="sec-testing_a_title_change"></div>
<h3><a id="sec-3_3_1" href="static-pages.html#sec-testing_a_title_change" class="heading"><span class="number">3.3.1</span> Testing a title change</a></h3>
<p>Our plan is to edit the Home, Help, and About pages to make page titles that change on each page. This will involve using the <code>&lt;title&gt;</code> tag in our page views. Most browsers display the contents of the title tag at the top of the browser window (Google Chrome is an odd exception), and it is also important for search-engine optimization. We&rsquo;ll start by writing tests for the titles, then add the titles themselves, and then use a <em>layout</em> file to refactor the resulting pages and eliminate duplication.</p>
<p>You may have noticed that the <code>rails new</code> command already created a layout file. We&rsquo;ll learn its purpose shortly, but for now you should rename it before proceeding:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> mv app/views/layouts/application.html.erb foobar <span class="c"># temporary change</span>
</pre></div>
</div>
<p>(<code>mv</code> is a Unix command; on Windows you may need to rename the file using the file browser or the <code>rename</code> command.) You wouldn&rsquo;t normally do this in a real application, but it&rsquo;s easier to understand the purpose of the layout file if we start by disabling it.</p>
<div class="label" id="table-static_pages"></div>
<div class="table"><div class="center">
<table class="tabular"><tr><th class="align_center"><strong>Page</strong></th><th class="align_left"><strong>URI</strong></th><th class="align_left"><strong>Base title</strong></th><th class="align_left"><strong>Variable title</strong></th></tr><tr class="top_bar"><td class="align_center">Home</td><td class="align_left">/static_pages/home</td><td class="align_left"><code>"Ruby on Rails Tutorial Sample App"</code></td><td class="align_left"><code>"Home"</code></td></tr><tr><td class="align_center">Help</td><td class="align_left">/static_pages/help</td><td class="align_left"><code>"Ruby on Rails Tutorial Sample App"</code></td><td class="align_left"><code>"Help"</code></td></tr><tr><td class="align_center">About</td><td class="align_left">/static_pages/about</td><td class="align_left"><code>"Ruby on Rails Tutorial Sample App"</code></td><td class="align_left"><code>"About"</code></td></tr></table></div><div class="caption"><span class="header">Table 3.1: </span><span class="description">The (mostly) static pages for the sample app.</span></div></div>
<p>By the end of this section, all three of our static pages will have titles of the form &ldquo;Ruby on Rails Tutorial Sample App | Home&rdquo;, where the last part of the title will vary depending on the page (<a class="ref" href="static-pages.html#table-static_pages">Table&nbsp;3.1</a>). We&rsquo;ll build on the tests in <a class="ref" href="static-pages.html#code-about_page_content_spec">Listing&nbsp;3.13</a>, adding title tests following the model in <a class="ref" href="static-pages.html#code-title_test">Listing&nbsp;3.17</a>.</p>
<div class="label" id="code-title_test"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.17.</span> <span class="description">A title test.</span> </div>
<div class="code"><div class="highlight"><pre><span class="n">it</span> <span class="s2">&quot;should have the right title&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/home&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span>
<span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">&quot;Ruby on Rails Tutorial Sample App | Home&quot;</span><span class="p">)</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>This uses the <code>have_selector</code> method, which checks for an HTML element (the &ldquo;selector&rdquo;) with the given content. In other words, the code</p>
<div class="code"><div class="highlight"><pre><span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span>
<span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">&quot;Ruby on Rails Tutorial Sample App | Home&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>checks to see that the content inside the <code>title</code> tag is</p>
<div class="code"><div class="highlight"><pre><span class="s2">&quot;Ruby on Rails Tutorial Sample App | Home&quot;</span>
</pre></div>
</div>
<p>(We&rsquo;ll learn in <a class="ref" href="rails-flavored-ruby.html#sec-hashes_and_symbols">Section&nbsp;4.3.3</a> that the <code>:text =&gt; "&hellip;"</code> syntax is a <em>hash</em> using a <em>symbol</em> as the key.) It&rsquo;s worth mentioning that the content need not be an exact match; any substring works as well, so that</p>
<div class="code"><div class="highlight"><pre><span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">&quot; | Home&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>will also match the full title.</p>
<p>Note that in <a class="ref" href="static-pages.html#code-title_test">Listing&nbsp;3.17</a> we&rsquo;ve broken the material inside <code>have_selector</code> into two lines; this tells you something important about Ruby syntax: Ruby doesn&rsquo;t care about newlines.<sup class="footnote" id="fnref-3_11"><a href="#fn-3_11">11</a></sup> The <em>reason</em> I chose to break the code into pieces is that I prefer to keep lines of source code under 80 characters for legibility.<sup class="footnote" id="fnref-3_12"><a href="#fn-3_12">12</a></sup> As it stands, this code formatting is still rather ugly; <a class="ref" href="static-pages.html#sec-static_pages_exercises">Section&nbsp;3.5</a> has a refactoring exercise that makes them prettier, and <a class="ref" href="filling-in-the-layout.html#sec-pretty_rspec">Section&nbsp;5.3.4</a> completely rewrites the StaticPages tests to take advantage of the latest features in RSpec.</p>
<p>Adding new tests for each of our three static pages, following the model of <a class="ref" href="static-pages.html#code-title_test">Listing&nbsp;3.17</a>, gives us our new StaticPages test (<a class="ref" href="static-pages.html#code-pages_controller_spec_title">Listing&nbsp;3.18</a>).</p>
<div class="label" id="code-pages_controller_spec_title"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.18.</span> <span class="description">The StaticPages controller spec with title tests. <br /> <code>spec/requests/static_pages_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">&#39;spec_helper&#39;</span>
<span class="n">describe</span> <span class="s2">&quot;Static pages&quot;</span> <span class="k">do</span>
<span class="n">describe</span> <span class="s2">&quot;Home page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the h1 &#39;Sample App&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/home&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s1">&#39;Sample App&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">it</span> <span class="s2">&quot;should have the title &#39;Home&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/home&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span>
<span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">&quot;Ruby on Rails Tutorial Sample App | Home&quot;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">&quot;Help page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the h1 &#39;Help&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/help&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s1">&#39;Help&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">it</span> <span class="s2">&quot;should have the title &#39;Help&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/help&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span>
<span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">&quot;Ruby on Rails Tutorial Sample App | Help&quot;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">&quot;About page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the h1 &#39;About Us&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/about&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s1">&#39;About Us&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">it</span> <span class="s2">&quot;should have the title &#39;About Us&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/about&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span>
<span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">&quot;Ruby on Rails Tutorial Sample App | About Us&quot;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Note that we&rsquo;ve changed <code>have_content</code> to the more specific <code>have_selector(&rsquo;h1&rsquo;, &hellip;)</code>. See if you can figure out why. (<em>Hint:</em> What would happen if the title contained, say, &lsquo;Help&rsquo;, but the content inside <code>h1</code> tag had &lsquo;Helf&rsquo; instead?)</p>
<p>With the tests from <a class="ref" href="static-pages.html#code-pages_controller_spec_title">Listing&nbsp;3.18</a> in place, you should run</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>to verify that our code is now Red (failing tests).</p>
<div class="label" id="sec-passing_title_tests"></div>
<h3><a id="sec-3_3_2" href="static-pages.html#sec-passing_title_tests" class="heading"><span class="number">3.3.2</span> Passing title tests</a></h3>
<p>Now we&rsquo;ll get our title tests to pass, and at the same time add the full HTML structure needed to make valid web pages. Let&rsquo;s start with the Home page (<a class="ref" href="static-pages.html#code-home_view_full_html">Listing&nbsp;3.19</a>), using the same basic HTML skeleton as in the &ldquo;hello&rdquo; page from <a class="ref" href="static-pages.html#code-hello_world">Listing&nbsp;3.3</a>.</p>
<div class="label" id="code-home_view_full_html"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.19.</span> <span class="description">The view for the Home page with full HTML structure. <br /> <code>app/views/static_pages/home.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;title&gt;</span>Ruby on Rails Tutorial Sample App | Home<span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
<span class="nt">&lt;h1&gt;</span>Sample App<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
This is the home page for the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails Tutorial<span class="nt">&lt;/a&gt;</span>
sample application.
<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div></div>
<p><a class="ref" href="static-pages.html#code-home_view_full_html">Listing&nbsp;3.19</a> uses the title tested for in <a class="ref" href="static-pages.html#code-pages_controller_spec_title">Listing&nbsp;3.18</a>:</p>
<div class="code"><div class="highlight"><pre><span class="nt">&lt;title&gt;</span>Ruby on Rails Tutorial Sample App | Home<span class="nt">&lt;/title&gt;</span>
</pre></div>
</div>
<p>As a result, the tests for the Home page should now pass. We&rsquo;re still Red because of the failing Help and About tests, and we can get to Green with the code in <a class="ref" href="static-pages.html#code-help_view_full_html">Listing&nbsp;3.20</a> and <a class="ref" href="static-pages.html#code-about_view_full_html">Listing&nbsp;3.21</a>.</p>
<div class="label" id="code-help_view_full_html"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.20.</span> <span class="description">The view for the Help page with full HTML structure. <br /> <code>app/views/static_pages/help.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;title&gt;</span>Ruby on Rails Tutorial Sample App | Help<span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
<span class="nt">&lt;h1&gt;</span>Help<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
Get help on the Ruby on Rails Tutorial at the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/help&quot;</span><span class="nt">&gt;</span>Rails Tutorial help page<span class="nt">&lt;/a&gt;</span>.
To get help on this sample app, see the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/book&quot;</span><span class="nt">&gt;</span>Rails Tutorial book<span class="nt">&lt;/a&gt;</span>.
<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div></div>
<div class="label" id="code-about_view_full_html"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.21.</span> <span class="description">The view for the About page with full HTML structure. <br /> <code>app/views/static_pages/about.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;title&gt;</span>Ruby on Rails Tutorial Sample App | About Us<span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
<span class="nt">&lt;h1&gt;</span>About Us<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
The <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails Tutorial<span class="nt">&lt;/a&gt;</span>
is a project to make a book and screencasts to teach web development
with <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://rubyonrails.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails<span class="nt">&lt;/a&gt;</span>. This
is the sample application for the tutorial.
<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div></div>
<div class="label" id="sec-embedded_ruby"></div>
<h3><a id="sec-3_3_3" href="static-pages.html#sec-embedded_ruby" class="heading"><span class="number">3.3.3</span> Embedded Ruby</a></h3>
<p>We&rsquo;ve achieved a lot already in this section, generating three valid pages using Rails controllers and actions, but they are purely static HTML and hence don&rsquo;t show off the power of Rails. Moreover, they suffer from terrible duplication:</p>
<ul>
<li>The page titles are almost (but not quite) exactly the same.</li>
<li>&ldquo;Ruby on Rails Tutorial Sample App&rdquo; is common to all three titles.</li>
<li>The entire HTML skeleton structure is repeated on each page.</li>
</ul>
<p>This repeated code is a violation of the important &ldquo;Don&rsquo;t Repeat Yourself&rdquo; (DRY) principle; in this section and the next we&rsquo;ll &ldquo;DRY out our code&rdquo; by removing the repetition.</p>
<p>Paradoxically, we&rsquo;ll take the first step toward eliminating duplication by first adding some more: we&rsquo;ll make the titles of the pages, which are currently quite similar, match <em>exactly</em>. This will make it much simpler to remove all the repetition at a stroke.</p>
<p>The technique involves using <em>Embedded Ruby</em> in our views. Since the Home, Help, and About page titles have a variable component, we&rsquo;ll use a special Rails function called <code>provide</code> to set a different title on each page. We can see how this works by replacing the literal title &ldquo;Home&rdquo; in the <code>home.html.erb</code> view with the code in <a class="ref" href="static-pages.html#code-home_view_erb_title">Listing&nbsp;3.22</a>.</p>
<div class="label" id="code-home_view_erb_title"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.22.</span> <span class="description">The view for the Home page with an Embedded Ruby title. <br /> <code>app/views/static_pages/home.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="n">provide</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="s1">&#39;Home&#39;</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;title&gt;</span>Ruby on Rails Tutorial Sample App | <span class="cp">&lt;%=</span> <span class="k">yield</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span> <span class="cp">%&gt;</span><span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
<span class="nt">&lt;h1&gt;</span>Sample App<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
This is the home page for the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails Tutorial<span class="nt">&lt;/a&gt;</span>
sample application.
<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div></div>
<p><a class="ref" href="static-pages.html#code-home_view_erb_title">Listing&nbsp;3.22</a> is our first example of Embedded Ruby, also called <em>ERb</em>. (Now you know why HTML views have the file extension <code>.html.erb</code>.) ERb is the primary template system for including dynamic content in web pages.<sup class="footnote" id="fnref-3_13"><a href="#fn-3_13">13</a></sup> The code</p>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="n">provide</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="s1">&#39;Home&#39;</span><span class="p">)</span> <span class="cp">%&gt;</span>
</pre></div>
</div>
<p>indicates using <tt class="verb">&lt;% ... %&gt;</tt> that Rails should call the <code>provide</code> function and associate the string <code>&rsquo;Home&rsquo;</code> with the label <code>:title</code>.<sup class="footnote" id="fnref-3_14"><a href="#fn-3_14">14</a></sup> Then, in the title, we use the closely related notation <tt class="verb">&lt;%= ... %&gt;</tt> to insert the title into the template using Ruby&rsquo;s <code>yield</code> function:<sup class="footnote" id="fnref-3_15"><a href="#fn-3_15">15</a></sup></p>
<div class="code"><div class="highlight"><pre><span class="nt">&lt;title&gt;</span>Ruby on Rails Tutorial Sample App | <span class="cp">&lt;%=</span> <span class="k">yield</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span> <span class="cp">%&gt;</span><span class="nt">&lt;/title&gt;</span>
</pre></div>
</div>
<p>(The distinction between the two types of embedded Ruby is that <tt class="verb">&lt;% ... %&gt;</tt> <em>executes</em> the code inside, while <tt class="verb">&lt;%= ... %&gt;</tt> executes it and <em>inserts</em> the result into the template.) The resulting page is exactly the same as before, only now the variable part of the title is generated dynamically by ERb.</p>
<p>We can verify that all this works by running the tests from <a class="ref" href="static-pages.html#sec-testing_a_title_change">Section&nbsp;3.3.1</a> and see that they still pass:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<p>Then we can make the corresponding replacements for the Help and About pages (<a class="ref" href="static-pages.html#code-help_view_erb_title">Listing&nbsp;3.23</a> and <a class="ref" href="static-pages.html#code-about_view_erb_title">Listing&nbsp;3.24</a>).</p>
<div class="label" id="code-help_view_erb_title"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.23.</span> <span class="description">The view for the Help page with an Embedded Ruby title. <br /> <code>app/views/static_pages/help.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="n">provide</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="s1">&#39;Help&#39;</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;title&gt;</span>Ruby on Rails Tutorial Sample App | <span class="cp">&lt;%=</span> <span class="k">yield</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span> <span class="cp">%&gt;</span><span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
<span class="nt">&lt;h1&gt;</span>Help<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
Get help on the Ruby on Rails Tutorial at the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/help&quot;</span><span class="nt">&gt;</span>Rails Tutorial help page<span class="nt">&lt;/a&gt;</span>.
To get help on this sample app, see the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/book&quot;</span><span class="nt">&gt;</span>Rails Tutorial book<span class="nt">&lt;/a&gt;</span>.
<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div></div>
<div class="label" id="code-about_view_erb_title"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.24.</span> <span class="description">The view for the About page with an Embedded Ruby title. <br /> <code>app/views/static_pages/about.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="n">provide</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="s1">&#39;About Us&#39;</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;title&gt;</span>Ruby on Rails Tutorial Sample App | <span class="cp">&lt;%=</span> <span class="k">yield</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span> <span class="cp">%&gt;</span><span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
<span class="nt">&lt;h1&gt;</span>About Us<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
The <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails Tutorial<span class="nt">&lt;/a&gt;</span>
is a project to make a book and screencasts to teach web development
with <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://rubyonrails.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails<span class="nt">&lt;/a&gt;</span>. This
is the sample application for the tutorial.
<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div></div>
<div class="label" id="sec-layouts"></div>
<h3><a id="sec-3_3_4" href="static-pages.html#sec-layouts" class="heading"><span class="number">3.3.4</span> Eliminating duplication with layouts</a></h3>
<p>Now that we&rsquo;ve replaced the variable part of the page titles with ERb, each of our pages looks something like this:</p>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="n">provide</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="s1">&#39;Foo&#39;</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;title&gt;</span>Ruby on Rails Tutorial Sample App | <span class="cp">&lt;%=</span> <span class="k">yield</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span> <span class="cp">%&gt;</span><span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
Contents
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div>
<p>In other words, <em>all</em> our pages are identical in structure, including the contents of the title tag, with the sole exception of the material inside the <code>body</code> tag.</p>
<p>In order to factor out this common structure, Rails comes with a special <em>layout</em> file called <code>application.html.erb</code>, which we renamed in <a class="ref" href="static-pages.html#sec-testing_a_title_change">Section&nbsp;3.3.1</a> and which we&rsquo;ll now restore:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> mv foobar app/views/layouts/application.html.erb
</pre></div>
</div>
<p>To get the layout to work, we have to replace the default title with the Embedded Ruby from the examples above:</p>
<div class="code"><div class="highlight"><pre><span class="nt">&lt;title&gt;</span>Ruby on Rails Tutorial Sample App | <span class="cp">&lt;%=</span> <span class="k">yield</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span> <span class="cp">%&gt;</span><span class="nt">&lt;/title&gt;</span>
</pre></div>
</div>
<p>The resulting layout appears in <a class="ref" href="static-pages.html#code-application_layout">Listing&nbsp;3.25</a>.</p>
<div class="label" id="code-application_layout"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.25.</span> <span class="description">The sample application site layout. <br /> <code>app/views/layouts/application.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;title&gt;</span>Ruby on Rails Tutorial Sample App | <span class="cp">&lt;%=</span> <span class="k">yield</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span> <span class="cp">%&gt;</span><span class="nt">&lt;/title&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">stylesheet_link_tag</span> <span class="s2">&quot;application&quot;</span><span class="p">,</span> <span class="ss">:media</span> <span class="o">=&gt;</span> <span class="s2">&quot;all&quot;</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">javascript_include_tag</span> <span class="s2">&quot;application&quot;</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">csrf_meta_tags</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
<span class="cp">&lt;%=</span> <span class="k">yield</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div></div>
<p>Note here the special line</p>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%=</span> <span class="k">yield</span> <span class="cp">%&gt;</span>
</pre></div>
</div>
<p>This code is responsible for inserting the contents of each page into the layout. It&rsquo;s not important to know exactly how this works; what matters is that using this layout ensures that, for example, visiting the page /static_pages/home converts the contents of <code>home.html.erb</code> to HTML and then inserts it in place of <tt class="verb">&lt;%= yield %&gt;</tt>.</p>
<p>It&rsquo;s also worth noting that the default Rails layout includes several additional lines:</p>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%=</span> <span class="n">stylesheet_link_tag</span> <span class="s2">&quot;application&quot;</span><span class="p">,</span> <span class="ss">:media</span> <span class="o">=&gt;</span> <span class="s2">&quot;all&quot;</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">javascript_include_tag</span> <span class="s2">&quot;application&quot;</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%=</span> <span class="n">csrf_meta_tags</span> <span class="cp">%&gt;</span>
</pre></div>
</div>
<p>This code arranges to include the application stylesheet and JavaScript, which are part of the asset pipeline (<a class="ref" href="filling-in-the-layout.html#sec-the_asset_pipeline">Section&nbsp;5.2.1</a>), together with the Rails method <code>csrf_meta_tags</code>, which prevents <a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">cross-site request forgery</a> (CSRF), a type of malicious web attack.</p>
<p>Of course, the views in <a class="ref" href="static-pages.html#code-home_view_erb_title">Listing&nbsp;3.22</a>, <a class="ref" href="static-pages.html#code-help_view_erb_title">Listing&nbsp;3.23</a>, and <a class="ref" href="static-pages.html#code-about_view_erb_title">Listing&nbsp;3.24</a> are still filled with all the HTML structure included in the layout, so we have to remove it, leaving only the interior contents. The resulting cleaned-up views appear in <a class="ref" href="static-pages.html#code-home_view_interior">Listing&nbsp;3.26</a>, <a class="ref" href="static-pages.html#code-help_view_interior">Listing&nbsp;3.27</a>, and <a class="ref" href="static-pages.html#code-about_view_interior">Listing&nbsp;3.28</a>.</p>
<div class="label" id="code-home_view_interior"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.26.</span> <span class="description">The Home page with HTML structure removed. <br /> <code>app/views/static_pages/home.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="n">provide</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="s1">&#39;Home&#39;</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;h1&gt;</span>Sample App<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
This is the home page for the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails Tutorial<span class="nt">&lt;/a&gt;</span>
sample application.
<span class="nt">&lt;/p&gt;</span>
</pre></div>
</div></div>
<div class="label" id="code-help_view_interior"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.27.</span> <span class="description">The Help page with HTML structure removed. <br /> <code>app/views/static_pages/help.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="n">provide</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="s1">&#39;Help&#39;</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;h1&gt;</span>Help<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
Get help on the Ruby on Rails Tutorial at the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/help&quot;</span><span class="nt">&gt;</span>Rails Tutorial help page<span class="nt">&lt;/a&gt;</span>.
To get help on this sample app, see the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/book&quot;</span><span class="nt">&gt;</span>Rails Tutorial book<span class="nt">&lt;/a&gt;</span>.
<span class="nt">&lt;/p&gt;</span>
</pre></div>
</div></div>
<div class="label" id="code-about_view_interior"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.28.</span> <span class="description">The About page with HTML structure removed. <br /> <code>app/views/static_pages/about.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="n">provide</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="s1">&#39;About Us&#39;</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;h1&gt;</span>About Us<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
The <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails Tutorial<span class="nt">&lt;/a&gt;</span>
is a project to make a book and screencasts to teach web development
with <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://rubyonrails.org/&quot;</span><span class="nt">&gt;</span>Ruby on Rails<span class="nt">&lt;/a&gt;</span>. This
is the sample application for the tutorial.
<span class="nt">&lt;/p&gt;</span>
</pre></div>
</div></div>
<p>With these views defined, the Home, Help, and About pages are the same as before, but they have much less duplication. Verifying that the test suite still passes gives us confidence that this code refactoring was successful:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle <span class="nb">exec </span>rspec spec/requests/static_pages_spec.rb
</pre></div>
</div>
<div class="label" id="sec-static_pages_conclusion"></div>
<h2><a id="sec-3_4" href="static-pages.html#sec-static_pages_conclusion" class="heading"><span class="number">3.4</span> Conclusion</a></h2>
<p>Seen from the outside, this chapter hardly accomplished anything: we started with static pages, and ended with&hellip; <em>mostly</em> static pages. But appearances are deceiving: by developing in terms of Rails controllers, actions, and views, we are now in a position to add arbitrary amounts of dynamic content to our site. Seeing exactly how this plays out is the task for the rest of this tutorial.</p>
<p>Before moving on, let&rsquo;s take a minute to commit our changes and merge them into the master branch. Back in <a class="ref" href="static-pages.html#sec-static_pages_with_rails">Section&nbsp;3.1.2</a> we created a Git branch for the development of static pages. If you haven&rsquo;t been making commits as we&rsquo;ve been moving along, first make a commit indicating that we&rsquo;ve reached a stopping point:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git add .
<span class="gp">$</span> git commit -m <span class="s2">&quot;Finish static pages&quot;</span>
</pre></div>
</div>
<p>Then merge the changes back into the master branch using the same technique as in <a class="ref" href="beginning.html#sec-git_commands">Section&nbsp;1.3.5</a>:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git checkout master
<span class="gp">$</span> git merge static-pages
</pre></div>
</div>
<p>Once you reach a stopping point like this, it&rsquo;s usually a good idea to push your code up to a remote repository (which, if you followed the steps in <a class="ref" href="beginning.html#sec-github">Section&nbsp;1.3.4</a>, will be GitHub):</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git push
</pre></div>
</div>
<p>If you like, at this point you can even deploy the updated application to Heroku:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> git push heroku
</pre></div>
</div>
<div class="label" id="sec-static_pages_exercises"></div>
<h2><a id="sec-3_5" href="static-pages.html#sec-static_pages_exercises" class="heading"><span class="number">3.5</span> Exercises</a></h2>
<ol>
<li>Make a Contact page for the sample app. Following the model in <a class="ref" href="static-pages.html#code-pages_controller_spec_title">Listing&nbsp;3.18</a>, first write a test for the existence of a page at the URI /static_pages/contact by testing for the right <code>h1</code> content, and then write a second test for the title &ldquo;Ruby on Rails Tutorial Sample App | Contact&rdquo;. Get your tests to pass, and then fill in the Contact page with the content from <a class="ref" href="static-pages.html#code-proposed_contact_page">Listing&nbsp;3.29</a>. (This exercise is solved as part of <a class="ref" href="filling-in-the-layout.html#sec-layout_links">Section&nbsp;5.3</a>.)</li>
<li>You may have noticed some repetition in the StaticPages controller spec (<a class="ref" href="static-pages.html#code-pages_controller_spec_title">Listing&nbsp;3.18</a>). In particular, the base title, &ldquo;Ruby on Rails Tutorial Sample App&rdquo;, is the same for every title test. Using the RSpec <code>let</code> function, which creates a variable corresponding to its argument, verify that the tests in <a class="ref" href="static-pages.html#code-pages_controller_spec_exercise">Listing&nbsp;3.30</a> still pass. <a class="ref" href="static-pages.html#code-pages_controller_spec_exercise">Listing&nbsp;3.30</a> introduces <em>string interpolation</em>, which is covered further in <a class="ref" href="rails-flavored-ruby.html#sec-strings">Section&nbsp;4.2.2</a>. </li>
<li><strong>(advanced)</strong> As noted on the <a href="http://devcenter.heroku.com/articles/how-do-i-use-sqlite3-for-development">Heroku page on using <tt>sqlite3</tt> for development</a>, it&rsquo;s a good idea to use the same database in development, test, and production environments to minimize the possibility of subtle incompatibilities. Follow the <a href="http://devcenter.heroku.com/articles/local-postgresql">Heroku instructions for local PostgreSQL installation</a> to install the PostgreSQL database on your local system. Update your <code>Gemfile</code> to eliminate the <tt>sqlite3</tt> gem and use the <tt>pg</tt> gem exclusively, as shown in <a class="ref" href="static-pages.html#code-Gemfile_pg_gem">Listing&nbsp;3.31</a>. You will also have to learn about the <code>config/database.yml</code> file and how to run PostgreSQL locally. Your goal should be to create and configure both the development database and the test database to use PostgreSQL. <strong>Warning:</strong> You may find this exercise challenging, and I recommend it only for advanced users. If you get stuck, don&rsquo;t hesitate to skip it; as noted previously, the sample application developed in this tutorial is fully compatible with both SQLite and PostgreSQL. </li>
</ol>
<div class="label" id="code-proposed_contact_page"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.29.</span> <span class="description">Code for a proposed Contact page. <br /> <code>app/views/static_pages/contact.html.erb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="cp">&lt;%</span> <span class="n">provide</span><span class="p">(</span><span class="ss">:title</span><span class="p">,</span> <span class="s1">&#39;Contact&#39;</span><span class="p">)</span> <span class="cp">%&gt;</span>
<span class="nt">&lt;h1&gt;</span>Contact<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p&gt;</span>
Contact Ruby on Rails Tutorial about the sample app at the
<span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;http://railstutorial.org/contact&quot;</span><span class="nt">&gt;</span>contact page<span class="nt">&lt;/a&gt;</span>.
<span class="nt">&lt;/p&gt;</span>
</pre></div>
</div></div>
<div class="label" id="code-pages_controller_spec_exercise"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.30.</span> <span class="description">The StaticPages controller spec with a base title. <br /> <code>spec/requests/static_pages_spec.rb</code></span> </div>
<div class="code"><div class="highlight"><pre><span class="nb">require</span> <span class="s1">&#39;spec_helper&#39;</span>
<span class="n">describe</span> <span class="s2">&quot;Static pages&quot;</span> <span class="k">do</span>
<span class="n">let</span><span class="p">(</span><span class="ss">:base_title</span><span class="p">)</span> <span class="p">{</span> <span class="s2">&quot;Ruby on Rails Tutorial Sample App&quot;</span> <span class="p">}</span>
<span class="n">describe</span> <span class="s2">&quot;Home page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the h1 &#39;Sample App&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/home&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s1">&#39;Sample App&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">it</span> <span class="s2">&quot;should have the title &#39;Home&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/home&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">base_title</span><span class="si">}</span><span class="s2"> | Home&quot;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">&quot;Help page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the h1 &#39;Help&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/help&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s1">&#39;Help&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">it</span> <span class="s2">&quot;should have the title &#39;Help&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/help&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">base_title</span><span class="si">}</span><span class="s2"> | Help&quot;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">&quot;About page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the h1 &#39;About Us&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/about&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s1">&#39;About Us&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">it</span> <span class="s2">&quot;should have the title &#39;About Us&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/about&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">base_title</span><span class="si">}</span><span class="s2"> | About Us&quot;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">describe</span> <span class="s2">&quot;Contact page&quot;</span> <span class="k">do</span>
<span class="n">it</span> <span class="s2">&quot;should have the h1 &#39;Contact&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/contact&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s1">&#39;Contact&#39;</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">it</span> <span class="s2">&quot;should have the title &#39;Contact&#39;&quot;</span> <span class="k">do</span>
<span class="n">visit</span> <span class="s1">&#39;/static_pages/contact&#39;</span>
<span class="n">page</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">base_title</span><span class="si">}</span><span class="s2"> | Contact&quot;</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="code-Gemfile_pg_gem"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.31.</span> <span class="description">The <code>Gemfile</code> needed to use PostgreSQL instead of SQLite.</span> </div>
<div class="code"><div class="highlight"><pre><span class="n">source</span> <span class="s1">&#39;https://rubygems.org&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;rails&#39;</span><span class="p">,</span> <span class="s1">&#39;3.2.8&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;pg&#39;</span><span class="p">,</span> <span class="s1">&#39;0.12.2&#39;</span>
<span class="n">group</span> <span class="ss">:development</span><span class="p">,</span> <span class="ss">:test</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;rspec-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;2.11.0&#39;</span>
<span class="k">end</span>
<span class="c1"># Gems used only for assets and not required</span>
<span class="c1"># in production environments by default.</span>
<span class="n">group</span> <span class="ss">:assets</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;sass-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;3.2.5&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;coffee-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;3.2.2&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;uglifier&#39;</span><span class="p">,</span> <span class="s1">&#39;1.2.3&#39;</span>
<span class="k">end</span>
<span class="n">gem</span> <span class="s1">&#39;jquery-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;2.0.2&#39;</span>
<span class="n">group</span> <span class="ss">:test</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;capybara&#39;</span><span class="p">,</span> <span class="s1">&#39;1.1.2&#39;</span>
<span class="k">end</span>
</pre></div>
</div></div>
<div class="label" id="sec-advanced_setup"></div>
<h2><a id="sec-3_6" href="static-pages.html#sec-advanced_setup" class="heading"><span class="number">3.6</span> Advanced setup</a></h2>
<p>As mentioned briefly in <a class="ref" href="static-pages.html#sec-first_tests">Section&nbsp;3.2</a>, using the <code>rspec</code> command directly is not ideal. In this section, we&rsquo;ll first discuss a method to eliminate the necessity of typing <code>bundle exec</code>, and then set up testing setup to automate the running of the test suite using Guard (<a class="ref" href="static-pages.html#sec-guard">Section&nbsp;3.6.2</a>) and, optionally, Spork (<a class="ref" href="static-pages.html#sec-spork">Section&nbsp;3.6.3</a>). Finally, we&rsquo;ll mention a method for running tests directly inside Sublime Text, a technique especially useful when used in concert with Spork.</p>
<p>This section should only be attempted by fairly advanced users and can be skipped without loss of continuity. Among other things, this material is likely to go out of date faster than the rest of the tutorial, so you shouldn&rsquo;t expect everything on your system to match the examples exactly, and you may have to Google around to get everything to work.</p>
<div class="label" id="sec-eliminating_bundle_exec"></div>
<h3><a id="sec-3_6_1" href="static-pages.html#sec-eliminating_bundle_exec" class="heading"><span class="number">3.6.1</span> Eliminating <tt>bundle exec</tt></a></h3>
<p>As mentioned briefly in <a class="ref" href="static-pages.html#sec-TDD">Section&nbsp;3.2.1</a>, it is necessary in general to prefix commands such as <code>rake</code> or <code>rspec</code> with <code>bundle exec</code> so that the programs run in the exact gem environment specified by the <code>Gemfile</code>. (For technical reasons, the only exception to this is the <code>rails</code> command itself.) This practice is rather cumbersome, and in this section we discuss two ways to eliminate its necessity.</p>
<div class="label" id="sec-rvm_bundler_integration"></div>
<h4><a id="sec-3_6_1_1" href="static-pages.html#sec-rvm_bundler_integration" class="heading">RVM Bundler integration</a></h4>
<p>The first and preferred method is to use RVM, which includes Bundler integration as of version 1.11. You can verify that you have a sufficiently up-to-date version of RVM as follows:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rvm get head <span class="o">&amp;&amp;</span> rvm reload
<span class="gp">$</span> rvm -v
<span class="go">rvm 1.15.6 (master)</span>
</pre></div>
</div>
<p>As long as the version number is 1.11.x or greater, installed gems will automatically be executed in the proper Bundler environment, so that you can write (for example)</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rspec spec/
</pre></div>
</div>
<p>and omit the leading <code>bundle exec</code>. If this is the case, you should skip the rest of this section.</p>
<p>If for any reason you are restricted to an earlier version of RVM, you can still eliminate <code>bundle exec</code> by using <a href="http://rvm.io/integration/bundler/">RVM Bundler integration</a><sup class="footnote" id="fnref-3_16"><a href="#fn-3_16">16</a></sup> to configure the Ruby Version Manager to include the proper executables automatically in the local environment. The steps are simple if somewhat mysterious. First, run these two commands:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> rvm get head <span class="o">&amp;&amp;</span> rvm reload
<span class="gp">$</span> chmod +x <span class="nv">$rvm_path</span>/hooks/after_cd_bundler
</pre></div>
</div>
<p>Then run these:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> <span class="nb">cd</span> ~/rails_projects/sample_app
<span class="gp">$</span> bundle install --without production --binstubs<span class="o">=</span>./bundler_stubs
</pre></div>
</div>
<p>Together, these commands combine RVM and Bundler magic to ensure that commands such as <code>rake</code> and <code>rspec</code> are automatically executed in the right environment. Since these files are specific to your local setup, you should add the <code>bundler_stubs</code> directory to your <code>.gitignore</code> file (<a class="ref" href="static-pages.html#code-bundler_stubs_gitignore">Listing&nbsp;3.32</a>).</p>
<div class="label" id="code-bundler_stubs_gitignore"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.32.</span> <span class="description">Adding <code>bundler_stubs</code> to the <code>.gitignore</code> file.</span> </div>
<div class="code"><div class="highlight"><pre># Ignore bundler config
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
# Ignore other unneeded files.
doc/
*.swp
*~
.project
.DS_Store
bundler_stubs/
</pre></div>
</div></div>
<p>If you add another executable (such as <code>guard</code> in <a class="ref" href="static-pages.html#sec-guard">Section&nbsp;3.6.2</a>), you should re-run the <code>bundle install</code> command:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle install --binstubs<span class="o">=</span>./bundler_stubs
</pre></div>
</div>
<div class="label" id="sec-binstubs"></div>
<h4><a id="sec-3_6_1_2" href="static-pages.html#sec-binstubs" class="heading">binstubs</a></h4>
<p>If you&rsquo;re not using RVM, you can still avoid typing <code>bundle exec</code>. Bundler allows the creation of the associated binaries as follows:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bundle --binstubs
</pre></div>
</div>
<p>(In fact, this step, with a different target directory, is also used when using RVM.) This command creates all the necessary executables in the <code>bin/</code> directory of the application, so that we can now run the test suite as follows:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bin/rspec spec/
</pre></div>
</div>
<p>The same goes for <code>rake</code>, etc.:</p>
<div class="code"><div class="highlight"><pre><span class="gp">$</span> bin/rake db:migrate
</pre></div>
</div>
<p>If you add another executable (such as <code>guard</code> in <a class="ref" href="static-pages.html#sec-guard">Section&nbsp;3.6.2</a>), you should re-run the <tt class="verb">bundle</tt> <tt class="verb">--binstubs</tt> command.</p>
<p>For the sake of readers who skip this section, the rest of this tutorial will err on the side of caution and explicitly use <code>bundle exec</code>, but of course you should feel free to use the more compact version if your system is properly configured.</p>
<div class="label" id="sec-guard"></div>
<h3><a id="sec-3_6_2" href="static-pages.html#sec-guard" class="heading"><span class="number">3.6.2</span> Automated tests with Guard</a></h3>
<p>One annoyance associated with using the <code>rspec</code> command is having to switch to the command line and run the tests by hand. (A second annoyance, the slow start-up time of the test suite, is addressed in <a class="ref" href="static-pages.html#sec-spork">Section&nbsp;3.6.3</a>.) In this section, we&rsquo;ll show how to use <a href="https://github.com/guard/guard">Guard</a> to automate the running of the tests. Guard monitors changes in the filesystem so that, for example, when we change the <code>static_pages_spec.rb</code> file only those tests get run. Even better, we can configure Guard so that when, say, the <code>home.html.erb</code> file is modified, the <code>static_pages_spec.rb</code> automatically runs.</p>
<p>First we add <code>guard-rspec</code> to the <code>Gemfile</code> (<a class="ref" href="static-pages.html#code-gemfile_guard">Listing&nbsp;3.33</a>).</p>
<div class="label" id="code-gemfile_guard"></div>
<div class="codelisting">
<div class="listing"><span class="header">Listing 3.33.</span> <span class="description">A <code>Gemfile</code> for the sample app, including Guard.</span> </div>
<div class="code"><div class="highlight"><pre><span class="n">source</span> <span class="s1">&#39;https://rubygems.org&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;rails&#39;</span><span class="p">,</span> <span class="s1">&#39;3.2.8&#39;</span>
<span class="n">group</span> <span class="ss">:development</span><span class="p">,</span> <span class="ss">:test</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;sqlite3&#39;</span><span class="p">,</span> <span class="s1">&#39;1.3.5&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;rspec-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;2.11.0&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;guard-rspec&#39;</span><span class="p">,</span> <span class="s1">&#39;1.2.1&#39;</span>
<span class="k">end</span>
<span class="c1"># Gems used only for assets and not required</span>
<span class="c1"># in production environments by default.</span>
<span class="n">group</span> <span class="ss">:assets</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;sass-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;3.2.5&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;coffee-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;3.2.2&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;uglifier&#39;</span><span class="p">,</span> <span class="s1">&#39;1.2.3&#39;</span>
<span class="k">end</span>
<span class="n">gem</span> <span class="s1">&#39;jquery-rails&#39;</span><span class="p">,</span> <span class="s1">&#39;2.0.2&#39;</span>
<span class="n">group</span> <span class="ss">:test</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;capybara&#39;</span><span class="p">,</span> <span class="s1">&#39;1.1.2&#39;</span>
<span class="c1"># System-dependent gems</span>
<span class="k">end</span>
<span class="n">group</span> <span class="ss">:production</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;pg&#39;</span><span class="p">,</span> <span class="s1">&#39;0.12.2&#39;</span>
<span class="k">end</span>
</pre></div>
</div></div>
<p>Then we have to replace the comment at the end of the test group with some system-dependent gems (OS&nbsp;X users may have to install <a href="http://growl.info/downloads#generaldownloads">Growl and growlnotify</a> as well):</p>
<div class="code"><div class="highlight"><pre><span class="c1"># Test gems on Macintosh OS X</span>
<span class="n">group</span> <span class="ss">:test</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;capybara&#39;</span><span class="p">,</span> <span class="s1">&#39;1.1.2&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;rb-fsevent&#39;</span><span class="p">,</span> <span class="s1">&#39;0.9.1&#39;</span><span class="p">,</span> <span class="ss">:require</span> <span class="o">=&gt;</span> <span class="kp">false</span>
<span class="n">gem</span> <span class="s1">&#39;growl&#39;</span><span class="p">,</span> <span class="s1">&#39;1.0.3&#39;</span>
<span class="k">end</span>
</pre></div>
</div>
<div class="code"><div class="highlight"><pre><span class="c1"># Test gems on Linux</span>
<span class="n">group</span> <span class="ss">:test</span> <span class="k">do</span>
<span class="n">gem</span> <span class="s1">&#39;capybara&#39;</span><span class="p">,</span> <span class="s1">&#39;1.1.2&#39;</span>
<span class="n">gem</span> <span class="s1">&#39;rb-inotify&#39;</span><span class="p">,</span> <span class="s1">&#39;0.8.8&#39;</span>