From 6ece2ee5ca316ac31c5e6cf32c104475b0149216 Mon Sep 17 00:00:00 2001
From: Oscar Del Ben <info@oscardelben.com>
Date: Thu, 19 Apr 2012 10:33:15 +0200
Subject: Update guide for rails 3.2.3, fix code download link.

---
 guides/source/getting_started.textile | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

(limited to 'guides/source/getting_started.textile')

diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile
index 59b12b49e5..775243a8ab 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.textile
@@ -13,8 +13,6 @@ endprologue.
 WARNING. This Guide is based on Rails 3.1. Some of the code shown here will not
 work in earlier versions of Rails.
 
-WARNING: The Edge version of this guide is currently being re-worked. Please excuse us while we re-arrange the place.
-
 h3. Guide Assumptions
 
 This guide is designed for beginners who want to get started with a Rails
@@ -73,7 +71,8 @@ h3. Creating a New Rails Project
 
 The best way to use this guide is to follow each step as it happens, no code or
 step needed to make this example application has been left out, so you can
-literally follow along step by step. You can get the complete code "here":https://github.com/lifo/docrails/tree/master/guides/code/getting_started.
+literally follow along step by step. You can get the complete code
+"here":https://github.com/lifo/docrails/tree/master/guides/code/getting_started.
 
 By following along with this guide, you'll create a Rails project called <tt>blog</tt>, a
 (very) simple weblog. Before you can start building the application, you need to
@@ -97,7 +96,7 @@ To verify that you have everything installed correctly, you should be able to ru
 $ rails --version
 </shell>
 
-If it says something like "Rails 3.2.2" you are ready to continue.
+If it says something like "Rails 3.2.3" you are ready to continue.
 
 h4. Creating the Blog Application
 
@@ -111,7 +110,7 @@ $ rails new blog
 
 This will create a Rails application called Blog in a directory called blog.
 
-TIP: You can see all of the switches that the Rails application builder accepts by running <tt>rails new -h</tt>.
+TIP: You can see all of the command line options that the Rails application builder accepts by running <tt>rails new -h</tt>.
 
 After you create the blog application, switch to its folder to continue work directly in that application:
 
-- 
cgit v1.2.3


From 57a995b13e6b2c0c4ca981f272e0e7ef2dc552e7 Mon Sep 17 00:00:00 2001
From: Oscar Del Ben <info@oscardelben.com>
Date: Thu, 19 Apr 2012 11:16:15 +0200
Subject: Put warning on getting started guide back

---
 guides/source/getting_started.textile | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

(limited to 'guides/source/getting_started.textile')

diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile
index 775243a8ab..8d79dc823d 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.textile
@@ -10,9 +10,11 @@ you should be familiar with:
 
 endprologue.
 
-WARNING. This Guide is based on Rails 3.1. Some of the code shown here will not
+WARNING. This Guide is based on Rails 3.2. Some of the code shown here will not
 work in earlier versions of Rails.
 
+WARNING: The Edge version of this guide is currently being re-worked. Please excuse us while we re-arrange the place.
+
 h3. Guide Assumptions
 
 This guide is designed for beginners who want to get started with a Rails
-- 
cgit v1.2.3


From 6f76bf19c41ab2deaa453e420ffe6c07daf6e9d9 Mon Sep 17 00:00:00 2001
From: Oscar Del Ben <info@oscardelben.com>
Date: Thu, 19 Apr 2012 11:33:25 +0200
Subject: Add Ruby 1.9.3 recommendation

---
 guides/source/getting_started.textile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

(limited to 'guides/source/getting_started.textile')

diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile
index 8d79dc823d..e12dcd474b 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.textile
@@ -27,8 +27,8 @@ prerequisites installed:
 TIP: Note that Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails
 3.0. Ruby Enterprise Edition have these fixed since release 1.8.7-2010.02
 though. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults
-on Rails 3.0, so if you want to use Rails 3 with 1.9.x jump on 1.9.2 for smooth
-sailing.
+on Rails 3.0, so if you want to use Rails 3 with 1.9.x jump on 1.9.2 or
+1.9.3 for smooth sailing.
 
 * The "RubyGems":http://rubyforge.org/frs/?group_id=126 packaging system
   ** If you want to learn more about RubyGems, please read the "RubyGems User Guide":http://docs.rubygems.org/read/book/1
-- 
cgit v1.2.3


From e2575b6db9531f535bd17640cd14ba2b2c3033d1 Mon Sep 17 00:00:00 2001
From: Oscar Del Ben <info@oscardelben.com>
Date: Thu, 19 Apr 2012 12:07:35 +0200
Subject: Fix broken images links

---
 guides/source/getting_started.textile | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

(limited to 'guides/source/getting_started.textile')

diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile
index e12dcd474b..708a2c8d42 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.textile
@@ -226,7 +226,6 @@ h4. Laying down the ground work
 
 The first thing that you are going to need to create a new post within the application is a place to do that. A great place for that would be at +/posts/new+. If you attempt to navigate to that now -- by visiting "http://localhost:3000/posts/new":http://localhost:3000/posts/new -- Rails will give you a routing error:
 
-
 !images/getting_started/routing_error_no_route_matches.png(A routing error, no route matches /posts/new)!
 
 This is because there is nowhere inside the routes for the application -- defined inside +config/routes.rb+ -- that defines this route. By default, Rails has no routes configured at all, and so you must define your routes as you need them.
@@ -241,7 +240,7 @@ This route is a super-simple route: it defines a new route that only responds to
 
 With the route defined, requests can now be made to +/posts/new+ in the application. Navigate to "http://localhost:3000/posts/new":http://localhost:3000/posts/new and you'll see another routing error:
 
-!images/getting_started/routing_error_no_controller.png(Another routing error, uninitialized constant PostsController)
+!images/getting_started/routing_error_no_controller.png(Another routing error, uninitialized constant PostsController)!
 
 This error is happening because this route need a controller to be defined. The route is attempting to find that controller so it can serve the request, but with the controller undefined, it just can't do that. The solution to this particular problem is simple: you need to create a controller called +PostsController+. You can do this by running this command:
 
@@ -260,7 +259,7 @@ A controller is simply a class that is defined to inherit from +ApplicationContr
 
 If you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new now, you'll get a new error:
 
-!images/getting_started/unknown_action_new_for_posts.png(Unknown action new for PostsController!)
+!images/getting_started/unknown_action_new_for_posts.png(Unknown action new for PostsController!)!
 
 This error indicates that Rails cannot find the +new+ action inside the +PostsController+ that you just generated. This is because when controllers are generated in Rails they are empty by default, unless you tell it you wanted actions during the generation process.
 
@@ -273,7 +272,7 @@ end
 
 With the +new+ method defined in +PostsController+, if you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new you'll see another error:
 
-!images/getting_started/template_is_missing_posts_new.png(Template is missing for posts/new)
+!images/getting_started/template_is_missing_posts_new.png(Template is missing for posts/new)!
 
 You're getting this error now because Rails expects plain actions like this one to have views associated with them to display their information. With no view available, Rails errors out.
 
@@ -347,7 +346,7 @@ By using the +post+ method rather than the +get+ method, Rails will define a rou
 
 With the form and the route for it defined now, you will be able to fill in the form and then click the submit button to begin the process of creating a new post, so go ahead and do that. When you submit the form, you should see a familiar error:
 
-!images/getting_started/unknown_action_create_for_posts(Unknown action create for PostsController)!
+!images/getting_started/unknown_action_create_for_posts.png(Unknown action create for PostsController)!
 
 You will now need to create the +create+ action within the +PostsController+ for this to work.
 
-- 
cgit v1.2.3


From a4508ed1dc255f3880706cd7389cd82a94403671 Mon Sep 17 00:00:00 2001
From: Oscar Del Ben <info@oscardelben.com>
Date: Thu, 19 Apr 2012 13:12:27 +0200
Subject: Add model creation step to getting started guide

---
 guides/source/getting_started.textile | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

(limited to 'guides/source/getting_started.textile')

diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile
index 708a2c8d42..3715601a5a 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.textile
@@ -302,7 +302,9 @@ When you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/n
 
 h4. The first form
 
-To create a form within this template, you will use a _form builder_. The primary form builder for Rails is provided by a helper method called +form_for+. To use this method, write this code into +app/views/posts/new.html.erb+:
+To create a form within this template, you will use a <em>form
+builder</em>. The primary form builder for Rails is provided by a helper
+method called +form_for+. To use this method, add this code into +app/views/posts/new.html.erb+:
 
 <erb>
 <%= form_for :post do |f| %>
@@ -385,10 +387,30 @@ If you re-submit the form one more time you'll now no longer get the missing tem
 
 This action is now displaying the parameters for the post that are coming in from the form. However, this isn't really all that helpful. Yes, you can see the parameters but nothing in particular is being done with them.
 
+h4. Creating the Post model
+
+Rails uses models to manage database objects, so if you want to save
+data to the database you'll have to create a model. In our blog
+application you want to save posts, so you'll create a +Post+ model.
+
+You can create a model with the following command:
+
+<shell>
+$ rails generate model Post title:string text:text
+</shell>
+
+With that command we told Rails that we want a +Post+ model, which in
+turn should have a title attribute of type string, and a text attribute
+of type text. Rails in turn responded by creating a bunch of files. For
+now, we're only interested in +app/models/post.rb+ and
++db/migrate/20120419084633_create_posts.rb+. The latter is responsible
+for creating the dabase structure, which is what we'll look at next.
+
 h4. Running a Migration
 
-One of the products of the +rails generate scaffold+ command is a _database
-migration_. Migrations are Ruby classes that are designed to make it simple to
+As we've just seen, +rails generate model+ created a _database
+migration_ file inside the +db/migrate+ directory.
+Migrations are Ruby classes that are designed to make it simple to
 create and modify database tables. Rails uses rake commands to run migrations,
 and it's possible to undo a migration after it's been applied to your database.
 Migration filenames include a timestamp to ensure that they're processed in the
@@ -401,9 +423,8 @@ yours will have a slightly different name), here's what you'll find:
 class CreatePosts < ActiveRecord::Migration
   def change
     create_table :posts do |t|
-      t.string :name
       t.string :title
-      t.text :content
+      t.text :text
 
       t.timestamps
     end
@@ -415,7 +436,7 @@ The above migration creates a method named +change+ which will be called when yo
 run this migration. The action defined in this method is also reversible, which
 means Rails knows how to reverse the change made by this migration, in case you
 want to reverse it later. When you run this migration it will create a
-+posts+ table with two string columns and a text column. It also creates two
++posts+ table with one string column and a text column. It also creates two
 timestamp fields to allow Rails to track post creation and update times. More
 information about Rails migrations can be found in the "Rails Database
 Migrations":migrations.html guide.
-- 
cgit v1.2.3


From 0d5a7ad84ddbcb4de7c327e39a7747e00b0cd89f Mon Sep 17 00:00:00 2001
From: Oscar Del Ben <info@oscardelben.com>
Date: Fri, 20 Apr 2012 09:59:30 +0200
Subject: Add "Saving data in the controller" section

---
 guides/source/getting_started.textile | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

(limited to 'guides/source/getting_started.textile')

diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile
index 3715601a5a..6de9cc39bc 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.textile
@@ -463,6 +463,33 @@ command will apply to the database defined in the +development+ section of your
 environment, for instance in production, you must explicitly pass it when
 invoking the command: <tt>rake db:migrate RAILS_ENV=production</tt>.
 
+h4. Saving data in the controller
+
+Back into +posts_controller+, we need to change the +create+ action
+to use the new +Post+ model to save data in the database. Open that file
+and change the +create+ action to look like the following:
+
+<ruby>
+def create
+  @post = Post.new(params[:post])
+
+  @post.save
+  redirect_to :action => :index
+end
+</ruby>
+
+Here's what's going on: every Rails model can be initialized with its
+respective attributes, which are automatically mapped to its
+database columns. In the first line we do just that (remember that
++params[:post]+ contains the attributes we're interested in). Then,
++@post.save+ is responsible for saving the model in the database.
+Finally, on the last line we redirect the user to the +index+ action,
+wich we have not defined yet.
+
+TIP: As we'll see later, +@post.save+ returns a boolean indicating
+wherever the model was saved or not, and you can (and usually do) take
+different actions depending on the result of calling +@post.save+.
+
 h4. Adding a Link
 
 To hook the posts up to the home page you've already created, you can add a link
-- 
cgit v1.2.3


From 2e2afc0ac64190261df4b05428afddea96c8628c Mon Sep 17 00:00:00 2001
From: Oscar Del Ben <info@oscardelben.com>
Date: Fri, 20 Apr 2012 12:06:47 +0200
Subject: Add show action in getting started guide

---
 guides/source/getting_started.textile | 54 +++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)

(limited to 'guides/source/getting_started.textile')

diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile
index 6de9cc39bc..b86f785e3d 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.textile
@@ -474,7 +474,7 @@ def create
   @post = Post.new(params[:post])
 
   @post.save
-  redirect_to :action => :index
+  redirect_to :action => :show, :id => @post.id
 end
 </ruby>
 
@@ -483,13 +483,63 @@ respective attributes, which are automatically mapped to its
 database columns. In the first line we do just that (remember that
 +params[:post]+ contains the attributes we're interested in). Then,
 +@post.save+ is responsible for saving the model in the database.
-Finally, on the last line we redirect the user to the +index+ action,
+Finally, on the last line we redirect the user to the +show+ action,
 wich we have not defined yet.
 
 TIP: As we'll see later, +@post.save+ returns a boolean indicating
 wherever the model was saved or not, and you can (and usually do) take
 different actions depending on the result of calling +@post.save+.
 
+h4. Showing posts
+
+Before trying to create a new post, let's add the +show+ action, which
+will be responsible for showing our posts. Open +config/routes.rb+
+and add the following route:
+
+<ruby>
+get "posts/:id" => "posts#show"
+</ruby>
+
+The special syntax +:id+ tells rails that this route expects an +:id+
+parameter, which in our case will be the id of the post. Note that this
+time we had to specify the actual mapping, +posts#show+ because
+otherwise Rails would not know which action to render.
+
+As we did before, we need to add the +show+ action in the
++posts_controller+ and its respective view.
+
+<ruby>
+def show
+  @post = Post.find(params[:id])
+end
+</ruby>
+
+A couple of things to note. We use +Post.find+ to find the post we're
+interested in. We also use an instance variable (prefixed by +@+) to
+hold our reference to the post object. We do this because Rails will pass all instance
+variables to the view.
+
+Now, create a new file +app/view/posts/show.html.erb+ with the following
+content:
+
+<erb>
+<p>
+  <strong>Title:</strong>
+  <%= @post.title %>
+</p>
+
+<p>
+  <strong>Text:</strong>
+  <%= @post.text %>
+</p>
+</erb>
+
+Finally, if you now go to
+"http://localhost:3000/posts/new":http://localhost:3000/posts/new you'll
+be able to create a post. Try it!
+
+!images/getting_started/show_action_for_posts.png(Show action for posts)!
+
 h4. Adding a Link
 
 To hook the posts up to the home page you've already created, you can add a link
-- 
cgit v1.2.3


From e7e72aa253d9cb3bef786a955794986d1f3ff871 Mon Sep 17 00:00:00 2001
From: Oscar Del Ben <info@oscardelben.com>
Date: Fri, 20 Apr 2012 12:53:18 +0200
Subject: Add index and links section to Getting started guide

---
 guides/source/getting_started.textile | 109 ++++++++++++++++++++++++++++++++--
 1 file changed, 105 insertions(+), 4 deletions(-)

(limited to 'guides/source/getting_started.textile')

diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile
index b86f785e3d..9f1a7db36f 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.textile
@@ -540,20 +540,121 @@ be able to create a post. Try it!
 
 !images/getting_started/show_action_for_posts.png(Show action for posts)!
 
-h4. Adding a Link
+h4. Listing all posts
 
-To hook the posts up to the home page you've already created, you can add a link
-to the home page. Open +app/views/welcome/index.html.erb+ and modify it as follows:
+We still need a way to list all our posts, so let's do that. As usual,
+we'll need a route, a controller action, and a view:
+
+<ruby>
+# Add to config/routes.rb
+get "posts" => "posts#index"
+
+# Add to app/controllers/posts_controller.rb
+def index
+  @posts = Post.all
+end
+</ruby>
+
++app/view/posts/index.html.erb+:
+
+<erb>
+<h1>Listing posts</h1>
+
+<table>
+  <tr>
+    <th>Title</th>
+    <th>Text</th>
+  </tr>
+
+<% @posts.each do |post| %>
+  <tr>
+    <td><%= post.title %></td>
+    <td><%= post.text %></td>
+  </tr>
+<% end %>
+</table>
+</erb>
+
+h4. Adding links
+
+You can now create, show, and list posts. But it's difficult to navigate
+through pages, so let's add some links.
+
+Open +app/views/welcome/index.html.erb+ and modify it as follows:
 
 <ruby>
 <h1>Hello, Rails!</h1>
-<%= link_to "My Blog", posts_path %>
+<%= link_to "My Blog", :controller => "posts" %>
 </ruby>
 
 The +link_to+ method is one of Rails' built-in view helpers. It creates a
 hyperlink based on text to display and where to go - in this case, to the path
 for posts.
 
+Let's add links to the other views as well.
+
+<erb>
+# app/views/posts/index.html.erb
+
+<h1>Listing posts</h1>
+
+<%= link_to 'New post', :action => :new %>
+
+<table>
+  <tr>
+    <th>Title</th>
+    <th>Text</th>
+    <th></th>
+  </tr>
+
+<% @posts.each do |post| %>
+  <tr>
+    <td><%= post.title %></td>
+    <td><%= post.text %></td>
+    <td><%= link_to 'Show', :action => :show, :id => post.id %></td>
+  </tr>
+<% end %>
+</table>
+
+# app/views/posts/new.html.erb
+
+<%= form_for :post do |f| %>
+  <p>
+    <%= f.label :title %><br>
+    <%= f.text_field :title %>
+  </p>
+
+  <p>
+    <%= f.label :text %><br>
+    <%= f.text_area :text %>
+  </p>
+
+  <p>
+    <%= f.submit %>
+  </p>
+<% end %>
+
+<%= link_to 'Back', :action => :index %>
+
+# app/views/posts/show.html.erb
+
+<p>
+  <strong>Title:</strong>
+  <%= @post.title %>
+</p>
+
+<p>
+  <strong>Text:</strong>
+  <%= @post.text %>
+</p>
+
+<%= link_to 'Back', :action => :index %>
+</erb>
+
+TIP: If you want to link to an action in the same controller, you don't
+need to specify the +:controller+ option, as Rails will use the current
+controller by default.
+
 h4. Working with Posts in the Browser
 
 Now you're ready to start working with posts. To do that, navigate to
-- 
cgit v1.2.3


From 504ba12e8cd256b08a43f8ee56c6e2b0272ce6cc Mon Sep 17 00:00:00 2001
From: Oscar Del Ben <info@oscardelben.com>
Date: Sat, 21 Apr 2012 12:01:33 +0200
Subject: Add model validation section to Getting Started guide

---
 guides/source/getting_started.textile | 108 +++++++++++++++++++++++++++-------
 1 file changed, 88 insertions(+), 20 deletions(-)

(limited to 'guides/source/getting_started.textile')

diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile
index 9f1a7db36f..4a460ab2fd 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.textile
@@ -655,25 +655,11 @@ TIP: If you want to link to an action in the same controller, you don't
 need to specify the +:controller+ option, as Rails will use the current
 controller by default.
 
-h4. Working with Posts in the Browser
-
-Now you're ready to start working with posts. To do that, navigate to
-"http://localhost:3000":http://localhost:3000/ and then click the "My Blog"
-link:
-
-!images/posts_index.png(Posts Index screenshot)!
-
-This is the result of Rails rendering the +index+ view of your posts. There
-aren't currently any posts in the database, but if you click the +New Post+ link
-you can create one. After that, you'll find that you can edit posts, look at
-their details, or destroy them. All of the logic and HTML to handle this was
-built by the single +rails generate scaffold+ command.
-
 TIP: In development mode (which is what you're working in by default), Rails
 reloads your application with every browser request, so there's no need to stop
 and restart the web server.
 
-Congratulations, you're riding the rails! Now it's time to see how it all works.
+Congratulations, you're riding the rails! Now it’s time to see how it all works.
 
 h4. The Model
 
@@ -697,17 +683,99 @@ Open the +app/models/post.rb+ file and edit it:
 
 <ruby>
 class Post < ActiveRecord::Base
-  validates :name,  :presence => true
   validates :title, :presence => true,
                     :length => { :minimum => 5 }
 end
 </ruby>
 
-These changes will ensure that all posts have a name and a title, and that the
-title is at least five characters long. Rails can validate a variety of
-conditions in a model, including the presence or uniqueness of columns, their
+These changes will ensure that all posts have a title that is at least five characters long.
+Rails can validate a variety of conditions in a model, including the presence or uniqueness of columns, their
 format, and the existence of associated objects. Validations are covered in detail
-in "Active Record Validations and Callbacks":active_record_validations_callbacks.html#validations-overview
+in "Active Record Validations and
+Callbacks":active_record_validations_callbacks.html#validations-overview
+
+If you open +posts_controller+ again, you'll notice that we don't check
+the result of calling +@post.save+, but now if we don't pass a valid
+title, +save+ will return false and we need to show the form back to the
+user. To do that, modify the +create+ action to look like the following:
+
+<ruby>
+def new
+  @post = Post.new
+end
+
+def create
+  @post = Post.new(params[:post])
+
+  if @post.save
+    redirect_to :action => :show, :id => @post.id
+  else
+    render 'new'
+  end
+end
+</ruby>
+
+Notice that I've also added +@post = Post.new+ to the +new+ action. I'll
+explain why I did that in the next section.
+
+Now, if validations fail and +save+ returns false, we show the form back
+to the user. Note that we use +render+ instead of +redirect_to+. We do
+that because +render+ will pass the +@post+ variable back to the form,
+which contains the error information that we need.
+
+If you reload
+"http://localhost:3000/posts/new":http://localhost:3000/posts/new and
+try to save a post without a title, Rails will send you back to the
+form, but that's not very useful. You need to tell the user that
+something went wrong. To do that, you'll modify
++app/views/posts/index.html.erb+ to check for error messages:
+
+<erb>
+<%= form_for :post do |f| %>
+  <% if @post.errors.any? %>
+  <div id="errorExplanation">
+    <h2><%= pluralize(@post.errors.count, "error") %> prohibited
+	    this post from being saved:</h2>
+    <ul>
+    <% @post.errors.full_messages.each do |msg| %>
+      <li><%= msg %></li>
+    <% end %>
+    </ul>
+  </div>
+  <% end %>
+  <p>
+    <%= f.label :title %><br>
+    <%= f.text_field :title %>
+  </p>
+
+  <p>
+    <%= f.label :text %><br>
+    <%= f.text_area :text %>
+  </p>
+
+  <p>
+    <%= f.submit %>
+  </p>
+<% end %>
+
+<%= link_to 'Back', :action => :index %>
+</erb>
+
+A few things are going on. We check if there are any errors with
++@post.errors.any?+, and if that returns true we show the list of all
+errors with +@post.errors.full_messages+. +pluralize+ is a rails helper
+that takes a number and a string as its arguments. If the number is
+greater than one, the string will be automatically pluralized.
+
+The reason why we added +@post = Post.new+ in +posts_controller+ is that
+otherwise +@post+ would be +nil+ in our view, and calling
++@post.errors.any?+ would throw an error.
+
+TIP: Rails automatically wraps fields that contain an error with a div
+with class +field_with_errors+. You can define a css rule to make them
+standout.
+
+
 
 h4. Using the Console
 
-- 
cgit v1.2.3


From 3da2b530aff28d4ea0272e36578188bb6869cbcc Mon Sep 17 00:00:00 2001
From: Oscar Del Ben <info@oscardelben.com>
Date: Sat, 21 Apr 2012 12:17:51 +0200
Subject: Add validation code to getting started guide and improve validation
 section

---
 guides/source/getting_started.textile | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

(limited to 'guides/source/getting_started.textile')

diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile
index 4a460ab2fd..f184004f80 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.textile
@@ -695,9 +695,8 @@ in "Active Record Validations and
 Callbacks":active_record_validations_callbacks.html#validations-overview
 
 If you open +posts_controller+ again, you'll notice that we don't check
-the result of calling +@post.save+, but now if we don't pass a valid
-title, +save+ will return false and we need to show the form back to the
-user. To do that, modify the +create+ action to look like the following:
+the result of calling +@post.save+. We need to change its behavior to
+show the form back to the user if any error occur:
 
 <ruby>
 def new
@@ -716,12 +715,12 @@ end
 </ruby>
 
 Notice that I've also added +@post = Post.new+ to the +new+ action. I'll
-explain why I did that in the next section.
+explain why I did that in the next section, for now add that to your
+controller as well.
 
-Now, if validations fail and +save+ returns false, we show the form back
-to the user. Note that we use +render+ instead of +redirect_to+. We do
-that because +render+ will pass the +@post+ variable back to the form,
-which contains the error information that we need.
+Also notice that we use +render+ instead of +redirect_to+ when +save+
+returns false. We can use +render+ so that the +@post+ object is passed
+back to the view.
 
 If you reload
 "http://localhost:3000/posts/new":http://localhost:3000/posts/new and
@@ -762,8 +761,10 @@ something went wrong. To do that, you'll modify
 </erb>
 
 A few things are going on. We check if there are any errors with
-+@post.errors.any?+, and if that returns true we show the list of all
-errors with +@post.errors.full_messages+. +pluralize+ is a rails helper
++@post.errors.any?+, and in that case we show a list of all
+errors with +@post.errors.full_messages+.
+
++pluralize+ is a rails helper
 that takes a number and a string as its arguments. If the number is
 greater than one, the string will be automatically pluralized.
 
@@ -775,7 +776,9 @@ TIP: Rails automatically wraps fields that contain an error with a div
 with class +field_with_errors+. You can define a css rule to make them
 standout.
 
+Now you'll get a nice error message when saving a post without title:
 
+!images/getting_started/form_with_errors.png(Form With Errors)!
 
 h4. Using the Console
 
-- 
cgit v1.2.3

