Skip to content

Commit 3da2b53

Browse files
committed
Add validation code to getting started guide and improve validation
section
1 parent 504ba12 commit 3da2b53

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed
20.3 KB
Loading

Diff for: guides/code/getting_started/app/controllers/posts_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def show
99
end
1010

1111
def new
12+
@post = Post.new
1213
end
1314

1415
def create

Diff for: guides/code/getting_started/app/views/posts/_form.html.erb

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
<%= form_for :post, :url => { :action => :create } do |f| %>
2+
<% if @post.errors.any? %>
3+
<div id="errorExplanation">
4+
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
5+
<ul>
6+
<% @post.errors.full_messages.each do |msg| %>
7+
<li><%= msg %></li>
8+
<% end %>
9+
</ul>
10+
</div>
11+
<% end %>
212
<p>
313
<%= f.label :title %><br>
414
<%= f.text_field :title %>

Diff for: guides/source/getting_started.textile

+13-10
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,8 @@ in "Active Record Validations and
695695
Callbacks":active_record_validations_callbacks.html#validations-overview
696696

697697
If you open +posts_controller+ again, you'll notice that we don't check
698-
the result of calling [email protected]+, but now if we don't pass a valid
699-
title, +save+ will return false and we need to show the form back to the
700-
user. To do that, modify the +create+ action to look like the following:
698+
the result of calling [email protected]+. We need to change its behavior to
699+
show the form back to the user if any error occur:
701700

702701
<ruby>
703702
def new
@@ -716,12 +715,12 @@ end
716715
</ruby>
717716

718717
Notice that I've also added +@post = Post.new+ to the +new+ action. I'll
719-
explain why I did that in the next section.
718+
explain why I did that in the next section, for now add that to your
719+
controller as well.
720720

721-
Now, if validations fail and +save+ returns false, we show the form back
722-
to the user. Note that we use +render+ instead of +redirect_to+. We do
723-
that because +render+ will pass the +@post+ variable back to the form,
724-
which contains the error information that we need.
721+
Also notice that we use +render+ instead of +redirect_to+ when +save+
722+
returns false. We can use +render+ so that the +@post+ object is passed
723+
back to the view.
725724

726725
If you reload
727726
"http://localhost:3000/posts/new":http://localhost:3000/posts/new and
@@ -762,8 +761,10 @@ something went wrong. To do that, you'll modify
762761
</erb>
763762

764763
A few things are going on. We check if there are any errors with
765-
[email protected]?+, and if that returns true we show the list of all
766-
errors with [email protected]_messages+. +pluralize+ is a rails helper
764+
[email protected]?+, and in that case we show a list of all
765+
errors with [email protected]_messages+.
766+
767+
+pluralize+ is a rails helper
767768
that takes a number and a string as its arguments. If the number is
768769
greater than one, the string will be automatically pluralized.
769770

@@ -775,7 +776,9 @@ TIP: Rails automatically wraps fields that contain an error with a div
775776
with class +field_with_errors+. You can define a css rule to make them
776777
standout.
777778

779+
Now you'll get a nice error message when saving a post without title:
778780

781+
!images/getting_started/form_with_errors.png(Form With Errors)!
779782

780783
h4. Using the Console
781784

0 commit comments

Comments
 (0)