Ruby on Rails Tutorial
Learn Rails by Example
- Chapter 1 From zero to deploy
- 1.1 Introduction
- 1.2 Up and running
- 1.3 Version control with Git
- 1.4 Deploying
- 1.5 Conclusion
- Chapter 2 A demo app
- 2.1 Planning the application
- 2.2 The Users resource
- 2.3 The Microposts resource
- 2.4 Conclusion
- Chapter 3 Mostly static pages
- 3.1 Static pages
- 3.2 Our first tests
- 3.3 Slightly dynamic pages
- 3.4 Conclusion
- 3.5 Exercises
- Chapter 4 Rails-flavored Ruby
- 4.1 Motivation
- 4.2 Strings and methods
- 4.3 Other data structures
- 4.4 Ruby classes
- 4.5 Exercises
- Chapter 5 Filling in the layout
- Chapter 6 Modeling and viewing users, part I
- 6.1 User model
- 6.2 User validations
- 6.3 Viewing users
- 6.4 Conclusion
- 6.5 Exercises
- Chapter 7 Modeling and viewing users, part II
- 7.1 Insecure passwords
- 7.2 Secure passwords
- 7.3 Better user views
- 7.4 Conclusion
- 7.5 Exercises
- Chapter 8 Sign up
- 8.1 Signup form
- 8.2 Signup failure
- 8.3 Signup success
- 8.4 RSpec integration tests
- 8.5 Conclusion
- 8.6 Exercises
- Chapter 9 Sign in, sign out
- 9.1 Sessions
- 9.2 Signin failure
- 9.3 Signin success
- 9.4 Signing out
- 9.5 Conclusion
- 9.6 Exercises
- Chapter 10 Updating, showing, and deleting users
- 10.1 Updating users
- 10.2 Protecting pages
- 10.3 Showing users
- 10.4 Destroying users
- 10.5 Conclusion
- 10.6 Exercises
- Chapter 11 User microposts
- 11.1 A Micropost model
- 11.2 Showing microposts
- 11.3 Manipulating microposts
- 11.4 Conclusion
- 11.5 Exercises
- Chapter 12 Following users
- 12.1 The Relationship model
- 12.2 A web interface for following and followers
- 12.3 The status feed
- 12.4 Conclusion
- 12.5 Exercises
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 Ruby on Rails Tutorial is what I used to switch back to Rails again.
Though I’ve worked my way through many Rails books, this is the one that finally made me “get” it. Everything is done very much “the Rails way”—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’s like to do a real-world project. The tutorial’s code examples are not in isolation.
The linear narrative is such a great format. Personally, I powered through Rails Tutorial 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’ll get the ultimate benefit.
Ruby on Rails Tutorial owes a lot to my previous Rails book, RailsSpace, and hence to my coauthor Aurelius Prochazka. I’d like to thank Aure both for the work he did on that book and for his support of this one. I’d also like to thank Debra Williams Cauley, my editor on both RailsSpace and Rails Tutorial; as long as she keeps taking me to baseball games, I’ll keep writing books for her.
I’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 E. Seguin, Amy Hoy, Dave Chelimsky, Pat Maddox, Tom Preston-Werner, Chris Wanstrath, Chad Fowler, Josh Susser, Obie Fernandez, Ian McFarland, Steven Bristol, 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—far too many to list—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 be.
About the author
Michael Hartl is a programmer, educator, and entrepreneur. Michael was coauthor of RailsSpace, a best-selling Rails tutorial book published in 2007, and was cofounder and lead developer of Insoshi, a popular social networking platform in Ruby on Rails. Previously, he taught theoretical and computational physics at the California Institute of Technology (Caltech), where he received the Lifetime Achievement Award for Excellence in Teaching. Michael is a graduate of Harvard College, has a Ph.D. in Physics from Caltech, and is an alumnus of the Y Combinator program.
Copyright and license
Copyright (c) 2010 Michael Hartl Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 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 "AS IS", 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.
/* * --------------------------------------------------------------------------- * "THE BEERWARE LICENSE" (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 someday, and you think * this stuff is worth it, you can buy me a beer in return. * --------------------------------------------------------------------------- */
In this chapter, we’ll develop a simple demonstration application to show off some of the power of Rails. The purpose is to get a high-level overview of Ruby on Rails programming (and web development in general) by rapidly generating an application using scaffold generators.1 As discussed in Box 1.1, the rest of the book will take the opposite approach, developing a full application incrementally and explaining each new concept as it arises, but for a quick overview (and some instant gratification) there is no substitute for scaffolding. The resulting demo app will allow us to interact with it through its URLs, giving us insight into the structure of a Rails application, including a first example of the REST architecture favored by Rails.
As with the forthcoming sample application, the demo app will consist of users and their associated microposts (thus constituting a minimalist Twitter-style app). The functionality will be utterly under-developed, and many of the steps will seem like magic, but worry not: the full sample app will develop a similar application from the ground up starting in Chapter 3, and I will provide plentiful forward-references to later material. In the mean time, have patience and a little faith—the whole point of this tutorial is to take you beyond this superficial, scaffold-driven approach to achieve a deeper understanding of Rails.
In this section we’ll outline our plans for the demo application. As in Section 1.2.3, we’ll start by generating the application skeleton using the
$ cd ~/rails_projects $ rails demo_app $ cd demo_app
Next, we’ll copy the .gitignore file from Listing 1.2 to the demo app’s Rails root directory:
$ git init $ cp ~/rails_projects/first_app/.gitignore . $ git add . $ git commit -m "Initial commit"
You can also optionally create a new repository (Figure 2.1) and push it up to GitHub:
$ git remote add origin firstname.lastname@example.org:<username>/demo_app.git $ git push origin master
Now we’re ready to start making the app itself. The typical first step when making a web application is to create a data model, which is a representation of the structures needed by our application. In our case, the demo app will be a stripped-down microblog, with only users and short (micro) posts. Thus, we’ll begin with a model for users of the app (Section 2.1.1), and then we’ll add a model for microposts (Section 2.1.2).
There are as many choices for a user data model as there are different registration forms on the web; we’ll go with a distinctly minimalist approach. Users of our demo app will have a unique
integer identifier called
id, a publicly viewable
name (of type
string), and an
string) that will double as a username. A summary of the data model for users appears in Figure 2.2.
The core of the micropost data model is even simpler than the one for users: a micropost has only an
id and a
content field for the micropost’s text (of type
string).2 There’s an additional complication, though: we want to associate each micropost with a particular user; we’ll accomplish this by recording the
user_id of the owner of the post. The results are shown in Figure 2.3.
In this section, we’ll implement the users data model in Section 2.1.1, along with a web interface to that model. The combination will constitute a Users resource, which will allow us to think of users as objects that can be created, read, updated, and deleted through the web via the HTTP protocol.
As promised in the introduction, our Users resource will be created by a scaffold generator program, which comes standard with each Rails project. The argument of the
scaffold command is the singular version of the resource name (in this case,
User), together with optional parameters for the data model’s attributes:3
$ script/generate scaffold User name:string email:string exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/users exists app/views/layouts/ exists test/functional/ exists test/unit/ create test/unit/helpers/ exists public/stylesheets/ create app/views/users/index.html.erb create app/views/users/show.html.erb create app/views/users/new.html.erb create app/views/users/edit.html.erb create app/views/layouts/users.html.erb create public/stylesheets/scaffold.css create app/controllers/users_controller.rb create test/functional/users_controller_test.rb create app/helpers/users_helper.rb create test/unit/helpers/users_helper_test.rb route map.resources :users dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/user.rb create test/unit/user_test.rb create test/fixtures/users.yml create db/migrate create db/migrate/<timestamp>_create_users.rb
email:string, we have arranged for the User model to have the form shown in Figure 2.2. (Note that there is no need to include a parameter for
id; it is created automatically by Rails.4)
To proceed with the demo application, we first need to migrate the database (a process mentioned briefly in Section 1.2.5):
$ rake db:migrate
This simply updates the database with our new
users data model. We’ll learn more about database migrations starting in Section 6.1.1.
With that, we can run the local web server using
and the demo application should be ready to go at http://localhost:3000/.
Visiting the root url http://localhost:3000/ shows the same default Rails page shown in Figure 1.5, but in generating the Users resource scaffolding we have also created a large number of pages for manipulating users. For example, the page for listing all users is at /users, and the page for making a new user is at /users/new.5 The rest of this section is dedicated to taking a whirlwind tour through these user pages. As we proceed, it may help to refer to Table 2.1, which shows the correspondence between pages and URLs.
We can create a user by entering name and email values in the text fields and then clicking the Create button. The result is the user show page, as seen in Figure 2.6. (The green welcome message is accomplished using the flash, which we’ll learn about in Section 8.3.3.) Note that the URL is /users/1; as you might suspect, the number
1 is simply the user’s
id attribute from Figure 2.2. In Section 7.3, this page will become the user’s profile.
To change a user’s information, we visit the edit page (Figure 2.7). By modifying the user information and clicking the Update button, we arrange to change the information for the user in the demo application (Figure 2.8). (As we’ll see in detail starting in Chapter 6, this user data is stored in a database back-end.) We’ll add user edit/update functionality to the sample application in Section 10.1.
Now we’ll create a second user by revisiting the new page and submitting a second set of user information; the resulting user index is shown in Figure 2.9. Section 10.3 will develop the user index into a more polished page for showing all users.
Now that we’ve completed a quick overview of the Users resource, let’s examine one particular part of it in the context of the Model-View-Controller (MVC) pattern introduced in Section 1.2.4. Our strategy will be to describe the results of a typical browser hit—a visit to the user index page at /users—in terms of MVC (Figure 2.11).
The steps in Figure 2.11
- The browser issues a request for the /users URL.
- Rails routes /users to the
indexaction in the Users controller.
indexaction asks the User model to retrieve all users (
- The User model pulls all the users from the database.
- The User model returns the list of users to the controller.
- The controller captures the users in the
@usersvariable, which is passed to the
- The view uses Embedded Ruby to render the page as HTML.
- The controller passes the HTML back to the browser.6
We start with a request issued from the browser—i.e., the result of typing a URL in the address bar or clicking on a link (step 1 in Figure 2.11). This request hits the Rails router (step 2), which dispatches to the proper controller action based on the URL (and, as we’ll see in Box 3.1, the type of request). The code to create the mapping of user URLs to controller actions for the Users resource appears in Listing 2.1;7 this code effectively sets up the table of URL/action pairs seen in Table 2.1.
ActionController::Routing::Routes.draw do |map| map.resources :users . . . end
The pages from the tour in Section 2.2.1 correspond to actions in the Users controller, which is a collection of related actions; the controller generated by the scaffolding is shown schematically in Listing 2.2. Note the notation
class UsersController < ApplicationController; this is an example of a Ruby class with inheritance. (We’ll discuss inheritance briefly in Section 2.3.4 and cover both subjects in more detail in Section 4.4.)
class UsersController < ApplicationController def index . . . end def show . . . end def new . . . end def create . . . end def edit . . . end def update . . . end def destroy . . . end end
You may notice that there are more actions than there are pages; the
edit actions all correspond to pages from Section 2.2.1, but there are additional
destroy actions as well. These actions don’t typically render pages (although they sometimes do); instead, their main purpose is to modify information about users in the database. This full suite of controller actions, summarized in Table 2.2, represents the implementation of the REST architecture in Rails (Box 2.1). Note from Table 2.2 that there is some overlap in the URLs; for example, both the user
show action and the
update action correspond to the URL /users/1. The difference between them is the HTTP request method they respond to. We’ll learn more about HTTP request methods starting in Section 3.2.2.
|GET||/users||page to list all users|
|GET||/users/1||page to show user with id |
|GET||/users/new||page to make a new user|
|POST||/users||create a new user|
|GET||/users/1/edit||page to edit user with id |
|PUT||/users/1||update user with id |
|DELETE||/users/1||delete user with id |
If you read much about Ruby on Rails web development, you’ll see a lot of references to “REST”, which is an acronym for REpresentational State Transfer. REST is an architectural style for developing distributed, networked systems and software applications such as the World Wide Web and web applications. Although REST theory is rather abstract, in the context of Rails applications REST means that most application components (such as users and microposts) are modeled as resources that can be created, read, updated, and deleted—operations that correspond both to the CRUD operations of relational databases and the four fundamental HTTP request methods: POST, GET, PUT, and DELETE. (We’ll learn more about HTTP requests in Section 3.2.2 and especially Box 3.1.)
As a Rails application developer, the RESTful style of development helps you make choices about which controllers and actions to write: you simply structure the application using resources that get created, read, updated, and deleted. In the case of users and microposts, this process is straightforward, since they are naturally resources in their own right. In Chapter 12, we’ll see an example where REST principles allow us to model a subtler problem, “following users”, in a natural and convenient way.
indexaction for the demo application.
class UsersController < ApplicationController def index @users = User.all end . . . end
index action has the line
@users = User.all (step 3), which asks the User model to retrieve a list of all the users from the database (step 4), and then places them in the variable
@users (pronounced “at-users”) (step 5). The User model itself appears in Listing 2.4; although it is rather plain, it comes equipped with a large amount of functionality because of inheritance (Section 2.3.4 and Section 4.4). In particular, by using the Rails library called Active Record, the code in Listing 2.4 arranges for
User.all to return all the users.
class User < ActiveRecord::Base end
@users variable is defined, the controller calls the view (step 6), shown in Listing 2.5. Variables that start with the
@ sign, called instance variables, are automatically available in the view; in this case, the
index.html.erb view in Listing 2.5 iterates through the
@users list and outputs a line of HTML for each one.9
<h1>Listing users</h1> <table> <tr> <th>Name</th> <th>Email</th> </tr> <% @users.each do |user| %> <tr> <td><%=h user.name %></td> <td><%=h user.email %></td> <td><%= link_to 'Show', user %></td> <td><%= link_to 'Edit', edit_user_path(user) %></td> <td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td> </tr> <% end %> </table> <br /> <%= link_to 'New user', new_user_path %>
The view converts its contents to HTML (step 7), which is then returned by the controller to the browser for display (step 8).
Though good for getting a general overview of Rails, the scaffold Users resource suffers from a number of severe weaknesses.
- No data validations. Our User model accepts data such as blank names and invalid email addresses without complaint.
- No authentication. We have no notion signing in or out, and no way to prevent any user from performing any operation.
- No tests. This isn’t technically true—the scaffolding includes rudimentary tests—but the generated tests are ugly and inflexible, and they don’t test for data validation, authentication, or any other custom requirements.
- No layout. There is no consistent site styling or navigation.
- No real understanding. If you understand the scaffold code, you probably shouldn’t be reading this book.
Having generated and explored the Users resource, we turn now to the associated Microposts resource. Throughout this section, I recommend comparing the elements of the Microposts resource with the analogous user elements from Section 2.2; you should see that the two resources parallel each other in many ways. The RESTful structure of Rails applications is best absorbed by this sort of repetition of form; indeed, seeing the parallel structure of Users and Microposts even at this early stage is one of the prime motivations for this chapter. (As we’ll see, writing applications more robust than the toy example in this chapter takes considerable effort—we won’t see the Microposts resource again until Chapter 11—and I didn’t want to defer its first appearance quite that far.)
$ script/generate scaffold Micropost content:string user_id:integer
To update our database with the new data model, we need to run a migration as in Section 2.2:
$ rake db:migrate
Now we are in a position to create microposts in the same way we created users in Section 2.2.1. As you might guess, the scaffold generator has updated the Rails routes file with a rule for Microposts resource, as seen in Listing 2.6.11 As with users, the
map.resources :microposts routing rule maps a micropost URLs to actions in the Microposts controller, as seen in Table 2.3.
ActionController::Routing::Routes.draw do |map| map.resources :users map.resources :microposts . . . end
|GET||/microposts||page to list all microposts|
|GET||/microposts/1||page to show micropost with id |
|GET||/microposts/new||page to make a new micropost|
|POST||/microposts||create a new micropost|
|GET||/microposts/1/edit||page to edit micropost with id |
|PUT||/microposts/1||update micropost with id |
|DELETE||/microposts/1||delete micropost with id |
The Microposts controller itself appears in schematic form Listing 2.7. Note that, apart from having
MicropostsController in place of
UsersController, Listing 2.7 is identical to the code in Listing 2.2. This is a reflection of the REST architecture common to both resources.
class MicropostsController < ApplicationController def index . . . end def show . . . end def new . . . end def create . . . end def edit . . . end def update . . . end def destroy . . . end end
At this point, go ahead and create a micropost or two, taking care to make sure that at least one has a
1 to match the id of the first user created in Section 2.2.1. The result should look something like Figure 2.13.
Any micropost worthy of the name should have some means of enforcing the length of the post. Implementing this constraint in Rails is easy with validations; to accept microposts with at most 140 characters (à la Twitter), we use a length validation. At this point, you should open the file
app/models/micropost.rb in your text editor or IDE and fill it with the contents of Listing 2.8.
class Micropost < ActiveRecord::Base validates_length_of :content, :maximum => 140 end
At this point, the code in Listing 2.8 may look rather mysterious—we’ll cover validations more thoroughly starting in Section 6.2—but its effects are readily apparent if we go to the new micropost page and enter more than 140 characters for the content of the post. As seen in Figure 2.14, Rails renders error messages indicating that the micropost’s content is too long.12 (We’ll learn more about error messages in Section 8.2.3.)
One of the most powerful features of Rails is the ability to form associations between different data models. In the case of our User model, each user potentially has many microposts. We can express this in code by updating the User and Micropost models as in Listing 2.9 and Listing 2.10.
class User < ActiveRecord::Base has_many :microposts end
class Micropost < ActiveRecord::Base belongs_to :user validates_length_of :content, :maximum => 140 end
We can visualize the result of this association in Figure 2.15. Because of the
user_id column in the
microposts table, Rails (using Active Record) can infer the microposts associated with each user.
In Chapter 11 and Chapter 12, we will use the association of users and microposts both to display all a user’s microposts and to construct a Twitter-like micropost feed. For now, we can examine the implications of the user-micropost association by using the console, which is a useful tool for interacting with Rails applications. We first invoke the console with
script/console at the command line, and then retrieve the first user from the database using
User.first (putting the results in the variable
$ script/console >> first_user = User.first => #<User id: 1, name: "Michael Hartl", email: "email@example.com", created_at: "2010-04-03 02:01:31", updated_at: "2010-04-03 02:01:31"> >> first_user.microposts => [#<Micropost id: 1, content: "First micropost!", user_id: 1, created_at: "2010-04-03 02:37:37", updated_at: "2010-04-03 02:37:37">, #<Micropost id: 2, content: "Second micropost", user_id: 1, created_at: "2010-04-03 02:38:54", updated_at: "2010-04-03 02:38:54">]
Here we have accessed the user’s microposts using the code
first_user.microposts: with this code, Active Record automatically returns all the microposts with
user_id equal to the id of
first_user (in this case,
1). We’ll learn much more about the association facilities in Active Record in Chapter 11 and Chapter 12.
We end our discussion of the demo application with a brief description of the controller and model class hierarchies in Rails. This discussion will only make much sense if you have some experience with object-oriented programming (OOP); if you haven’t studied OOP, feel free to skip this section. In particular, if you are unfamiliar with classes (discussed in Section 4.4), I suggest looping back to this section at a later time.
We start with the inheritance structure for models. Comparing Listing 2.11 and Listing 2.12, we see that both the User model and the Micropost model inherit (via the left angle bracket
ActiveRecord::Base, which is the base class for models provided by ActiveRecord; a diagram summarizing this relationship appears in Figure 2.16. It is by inheriting from
ActiveRecord::Base that our model objects gain the ability to communicate with the database, treat the database columns as Ruby attributes, and so on.
Userclass, with inheritance.
class User < ActiveRecord::Base . . . end
Micropostclass, with inheritance.
class Micropost < ActiveRecord::Base . . . end
The inheritance structure for controllers is only slightly more complicated. Comparing Listing 2.13 and Listing 2.14, we see that both the Users controller and the Microposts controller inherit from the Application controller. Examining Listing 2.15, we see that
ApplicationController itself inherits from
ActionController::Base; this is the base class for controllers provided by the Rails library Action Pack. The relationships between these classes is illustrated in Figure 2.17.
UsersControllerclass, with inheritance.
class UsersController < ApplicationController . . . end
MicropostsControllerclass, with inheritance.
class MicropostsController < ApplicationController . . . end
ApplicationControllerclass, with inheritance.
class ApplicationController < ActionController::Base . . . end
As with model inheritance, by inheriting ultimately from
ActionController::Base both the Users and Microposts controllers gain a large amount of functionality, such as the ability to manipulate model objects, filter inbound HTTP requests, and render views as HTML. Since all Rails controllers inherit from
ApplicationController, rules defined in the Application controller automatically apply to every action in the application. For example, in Section 8.2.4 we’ll define a rule in the Application controller to filter passwords from all the Rails log files, thereby avoiding a serious potential security breach.
With the completion of the Microposts resource, now is a good time to push the repository up to GitHub:13
$ git add . $ git commit -a -m "Done with the demo app" $ git push
You can also deploy the demo app to Heroku:
$ heroku create $ git push heroku master $ heroku rake db:migrate
Note the final line here, which runs the database migrations on the Heroku server. This updates the database at Heroku with the necessary user/micropost data model. If you want to push the data up, too, you can do so using the
taps gem and
$ [sudo] gem install taps $ heroku db:push
We’ve come now to the end of the 30,000-foot view of a Rails application. The demo app developed in this chapter has several strengths and a host of weaknesses.
- High-level overview of Rails
- Introduction to MVC
- First taste of the REST architecture
- Beginning data modeling
- A live, database-backed web application in production14
- No custom layout or styling
- No static pages (like “Home” or “About”)
- No user passwords
- No user images
- No signing in
- No security
- No automatic user/micropost association
- No notion of “following” or “followed”
- No micropost feed
- No test-driven development
- No real understanding
The rest of this tutorial is dedicated to building on the strengths and eliminating the weaknesses.
- I urge you not to look too closely at the generated code; at this stage, it will only serve to confuse you. ↑
- When modeling longer posts, such as those for a normal (non-micro) blog, you should use the
texttype in place of
- The name of the scaffold follows the convention of models, which are singular, rather than resources and controllers, which are plural. Thus, we have
- The user id is needed as the primary key in the database. ↑
- Since the http://localhost:3000 part of the address is implicit whenever we are developing locally, I’ll usually omit it from now on. ↑
- Some references indicate that the view returns the HTML directly to the browser (via a web server such as Apache or Nginx). Regardless of the implementation details, I prefer to think of the controller as a central hub through which all the application’s information flows. ↑
- The strange notation
:usersis a symbol, which we’ll learn about in Section 4.3.3. ↑
- The scaffold code is ugly and confusing, so I’ve suppressed it. ↑
- Remember, you aren’t supposed to understand this code right now. It is shown only for purposes of illustration. ↑
- As with the User scaffold, the scaffold generator for microposts follows the singular convention of Rails models; thus, we have
generate Micropost. ↑
- The scaffold code may have extra newlines compared to Listing 2.6; this is not a cause for concern, as Ruby ignores extra newlines. ↑
- You might notice that the HTML Validator indicates an error; annoyingly, the default Rails error messages are not valid HTML. ↑
- Ordinarily, you should make smaller, more frequent commits, but for the purposes of this chapter a single big commit at the end is just fine. ↑
- If you deployed to Heroku in Section 2.3.5. ↑