From 2e91fb84f0de5fa08a2ed03f3b74054b441788d3 Mon Sep 17 00:00:00 2001 From: Mike Gunderloy Date: Sun, 19 Oct 2008 16:57:02 -0500 Subject: Fixes to Getting Started guide --- .../getting_started_with_rails.txt | 36 ++++++++++++++-------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'railties') diff --git a/railties/doc/guides/getting_started_with_rails/getting_started_with_rails.txt b/railties/doc/guides/getting_started_with_rails/getting_started_with_rails.txt index b6d8203c8b..a58ef01e46 100644 --- a/railties/doc/guides/getting_started_with_rails/getting_started_with_rails.txt +++ b/railties/doc/guides/getting_started_with_rails/getting_started_with_rails.txt @@ -417,10 +417,9 @@ Rails includes methods to help you validate the data that you send to models. Op [source, ruby] ------------------------------------------------------- -class Comment < ActiveRecord::Base - belongs_to :post - validates_presence_of :commenter, :body - validates_length_of :commenter, :minimum => 5 +class Post < ActiveRecord::Base + validates_presence_of :name, :title + validates_length_of :title, :minimum => 5 end ------------------------------------------------------- @@ -439,19 +438,19 @@ After the console loads, you can use it to work with your application's models: [source, shell] ------------------------------------------------------- ->> c = Comment.create(:body => "A new comment") -=> # ->> c.save +>> p = Post.create(:content => "A new post") +=> # +>> p.save => false ->> c.errors -=> #, @errors={"commenter"=>["can't be blank", +>> p.errors +=> #, +@errors={"name"=>["can't be blank"], "title"=>["can't be blank", "is too short (minimum is 5 characters)"]}> ------------------------------------------------------- -This code shows creating a new +Comment+ instance, attempting to save it and getting +false+ for a return value (indicating that the save failed), and inspecting the +errors+ of the comment. +This code shows creating a new +Post+ instance, attempting to save it and getting +false+ for a return value (indicating that the save failed), and inspecting the +errors+ of the comment. TIP: Unlike the development web server, the console does not automatically load your code afresh for each line. If you make changes, type +reload!+ at the console prompt to load them. @@ -818,6 +817,8 @@ You'll need to edit the +post.rb+ file to add the other side of the association: [source, ruby] ------------------------------------------------------- class Post < ActiveRecord::Base + validates_presence_of :name, :title + validates_length_of :title, :minimum => 5 has_many :comments end ------------------------------------------------------- @@ -930,6 +931,15 @@ end You'll see a bit more complexity here than you did in the controller for posts. That's a side-effect of the nesting that you've set up; each request for a comment has to keep track of the post to which the comment is attached. +In addition, the code takes advantage of some of the methods available for an association. For example, in the +new+ method, it calls + +[source, ruby] +------------------------------------------------------- +@comment = @post.comments.build +------------------------------------------------------- + +This creates a new +Comment+ object _and_ sets up the +post_id+ field to have the +id+ from the specified +Post+ object in a single operation. + === Building Views Because you skipped scaffolding, you'll need to build views for comments "by hand." Invoking +script/generate controller+ will give you skeleton views, but they'll be devoid of actual content. Here's a first pass at fleshing out the comment views. -- cgit v1.2.3