aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--guides/assets/images/getting_started/form_with_errors.pngbin0 -> 20820 bytes
-rw-r--r--guides/code/getting_started/app/controllers/posts_controller.rb1
-rw-r--r--guides/code/getting_started/app/views/posts/_form.html.erb10
-rw-r--r--guides/source/getting_started.textile23
4 files changed, 24 insertions, 10 deletions
diff --git a/guides/assets/images/getting_started/form_with_errors.png b/guides/assets/images/getting_started/form_with_errors.png
new file mode 100644
index 0000000000..badefe6ea6
--- /dev/null
+++ b/guides/assets/images/getting_started/form_with_errors.png
Binary files differ
diff --git a/guides/code/getting_started/app/controllers/posts_controller.rb b/guides/code/getting_started/app/controllers/posts_controller.rb
index 2ad69a9bcf..947cd2a767 100644
--- a/guides/code/getting_started/app/controllers/posts_controller.rb
+++ b/guides/code/getting_started/app/controllers/posts_controller.rb
@@ -9,6 +9,7 @@ class PostsController < ApplicationController
end
def new
+ @post = Post.new
end
def create
diff --git a/guides/code/getting_started/app/views/posts/_form.html.erb b/guides/code/getting_started/app/views/posts/_form.html.erb
index 51fa3f4f12..18cb29f335 100644
--- a/guides/code/getting_started/app/views/posts/_form.html.erb
+++ b/guides/code/getting_started/app/views/posts/_form.html.erb
@@ -1,4 +1,14 @@
<%= form_for :post, :url => { :action => :create } 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 %>
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