aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/getting_started.textile
diff options
context:
space:
mode:
Diffstat (limited to 'railties/guides/source/getting_started.textile')
-rw-r--r--railties/guides/source/getting_started.textile90
1 files changed, 50 insertions, 40 deletions
diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile
index a4f969efe9..7e3f2cc931 100644
--- a/railties/guides/source/getting_started.textile
+++ b/railties/guides/source/getting_started.textile
@@ -542,7 +542,7 @@ The +respond_to+ block handles both HTML and XML calls to this action. If you br
This view iterates over the contents of the +@posts+ array to display content and links. A few things to note in the view:
* +link_to+ builds a hyperlink to a particular destination
-* +edit_post_path+ is a helper that Rails provides as part of RESTful routing. You'll see a variety of these helpers for the different actions that the controller includes.
+* +edit_post_path+ and +new_post_path+ are helpers that Rails provides as part of RESTful routing. You'll see a variety of these helpers for the different actions that the controller includes.
NOTE. In previous versions of Rails, you had to use +<%=h post.name %>+ so that any HTML would be escaped before being inserted into the page. In Rails 3.0, this is now the default. To get unescaped HTML, you now use +<%= raw post.name %>+.
@@ -662,7 +662,7 @@ def create
end
</ruby>
-The +create+ action instantiates a new Post object from the data supplied by the user on the form, which Rails makes available in the +params+ hash. After successfully saving the new post, returns the appropriate format that the user has requested (HTML in our case). It then redirects the user to the resulting post +show+ action and sets a notice to the user that the Post was successfully created.
+The +create+ action instantiates a new Post object from the data supplied by the user on the form, which Rails makes available in the +params+ hash. After successfully saving the new post, +create+ returns the appropriate format that the user has requested (HTML in our case). It then redirects the user to the resulting post +show+ action and sets a notice to the user that the Post was successfully created.
If the post was not successfully saved, due to a validation error, then the controller returns the user back to the +new+ action with any error messages so that the user has the chance to fix the error and try again.
@@ -689,17 +689,17 @@ The +show+ action uses +Post.find+ to search for a single record in the database
<p class="notice"><%= notice %></p>
<p>
- <strong>Name:</strong>
+ <b>Name:</b>
<%= @post.name %>
</p>
<p>
- <strong>Title:</strong>
+ <b>Title:</b>
<%= @post.title %>
</p>
<p>
- <strong>Content:</strong>
+ <b>Content:</b>
<%= @post.content %>
</p>
@@ -752,7 +752,7 @@ def update
end
</ruby>
-In the +update+ action, Rails first uses the +:id+ parameter passed back from the edit view to locate the database record that's being edited. The +update_attributes+ call then takes the rest of the parameters from the request and applies them to this record. If all goes well, the user is redirected to the post's +show+ view. If there are any problems, it's back to +edit+ to correct them.
+In the +update+ action, Rails first uses the +:id+ parameter passed back from the edit view to locate the database record that's being edited. The +update_attributes+ call then takes the rest of the parameters from the request and applies them to this record. If all goes well, the user is redirected to the post's +show+ view. If there are any problems, it's back to the +edit+ view to correct them.
h4. Destroying a Post
@@ -774,7 +774,7 @@ The +destroy+ method of an Active Record model instance removes the correspondin
h3. Adding a Second Model
-Now that you've seen what's in a model built with scaffolding, it's time to add a second model to the application. The second model will handle comments on blog posts.
+Now that you've seen how a model built with scaffolding looks like, it's time to add a second model to the application. The second model will handle comments on blog posts.
h4. Generating a Model
@@ -904,17 +904,17 @@ So first, we'll wire up the Post show template (+/app/views/posts/show.html.erb+
<p class="notice"><%= notice %></p>
<p>
- <strong>Name:</strong>
+ <b>Name:</b>
<%= @post.name %>
</p>
<p>
- <strong>Title:</strong>
+ <b>Title:</b>
<%= @post.title %>
</p>
<p>
- <strong>Content:</strong>
+ <b>Content:</b>
<%= @post.content %>
</p>
@@ -961,29 +961,29 @@ Once we have made the new comment, we send the user back to the original post us
<p class="notice"><%= notice %></p>
<p>
- <strong>Name:</strong>
+ <b>Name:</b>
<%= @post.name %>
</p>
<p>
- <strong>Title:</strong>
+ <b>Title:</b>
<%= @post.title %>
</p>
<p>
- <strong>Content:</strong>
+ <b>Content:</b>
<%= @post.content %>
</p>
<h2>Comments</h2>
<% @post.comments.each do |comment| %>
<p>
- <strong>Commenter:</strong>
+ <b>Commenter:</b>
<%= comment.commenter %>
</p>
<p>
- <strong>Comment:</strong>
+ <b>Comment:</b>
<%= comment.body %>
</p>
<% end %>
@@ -1023,12 +1023,12 @@ First we will make a comment partial to extract showing all the comments for the
<erb>
<p>
- <strong>Commenter:</strong>
+ <b>Commenter:</b>
<%= comment.commenter %>
</p>
<p>
- <strong>Comment:</strong>
+ <b>Comment:</b>
<%= comment.body %>
</p>
</erb>
@@ -1039,17 +1039,17 @@ Then in the +app/views/posts/show.html.erb+ you can change it to look like the f
<p class="notice"><%= notice %></p>
<p>
- <strong>Name:</strong>
+ <b>Name:</b>
<%= @post.name %>
</p>
<p>
- <strong>Title:</strong>
+ <b>Title:</b>
<%= @post.title %>
</p>
<p>
- <strong>Content:</strong>
+ <b>Content:</b>
<%= @post.content %>
</p>
@@ -1110,17 +1110,17 @@ Then you make the +app/views/posts/show.html.erb+ look like the following:
<p class="notice"><%= notice %></p>
<p>
- <strong>Name:</strong>
+ <b>Name:</b>
<%= @post.name %>
</p>
<p>
- <strong>Title:</strong>
+ <b>Title:</b>
<%= @post.title %>
</p>
<p>
- <strong>Content:</strong>
+ <b>Content:</b>
<%= @post.content %>
</p>
@@ -1149,12 +1149,12 @@ So first, let's add the delete link in the +app/views/comments/_comment.html.erb
<erb>
<p>
- <strong>Commenter:</strong>
+ <b>Commenter:</b>
<%= comment.commenter %>
</p>
<p>
- <strong>Comment:</strong>
+ <b>Comment:</b>
<%= comment.body %>
</p>
@@ -1191,7 +1191,7 @@ The +destroy+ action will find the post we are looking at, locate the comment wi
h4. Deleting Associated Objects
-If you decide at some point to delete a post, you likely want to delete the comments associated with that post as well. You can do so by taking advantage of the association option +dependent+. All you need to do is modify the +post.rb+ as follows:
+If you delete a post then it's associated comments will also need to be deleted. Otherwise they would simply occupy space in the database. Rails allows you to use the +dependent+ option of an association to achieve this. Modify the Post model, +app/models/post.rb+, as follows:
<ruby>
class Post < ActiveRecord::Base
@@ -1204,9 +1204,9 @@ end
h3. Security
-Before you publish your blog online, you will most likely want to prevent just anyone from being able to add, edit and delete posts or delete comments.
+If you were to publish your blog online, anybody would be able to add, edit and delete posts or delete comments.
-Rails provides a very simple http authentication system that will work nicely in this situation. First, we enable simple HTTP based authentication in our <tt>app/controllers/application_controller.rb</tt>:
+Rails provides a very simple HTTP authentication system that will work nicely in this situation. First, we enable simple HTTP based authentication in our <tt>app/controllers/application_controller.rb</tt>:
<ruby>
class ApplicationController < ActionController::Base
@@ -1261,7 +1261,7 @@ Now if you try to create a new post, you will be greeted with a basic HTTP Authe
h3. Building a Multi-Model Form
-Another piece of your average blog is the ability to tag posts. This requires that your application edits more than one thing on a single form. Rails offers support for nested forms.
+Another feature of your average blog is the ability to tag posts. To implement this feature your application needs to interact with more than one model on a single form. Rails offers support for nested forms.
To demonstrate this, we will add support for giving each post multiple tags, right in the form where you create the post. First, create a new model to hold the tags:
@@ -1298,7 +1298,16 @@ We will modify +views/posts/_form.html.erb+ to render a partial to make a tag:
<erb>
<% @post.tags.build %>
<%= form_for(@post) do |post_form| %>
- <%= post_form.error_messages %>
+ <% 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 %>
<div class="field">
<%= post_form.label :name %><br />
@@ -1321,9 +1330,9 @@ We will modify +views/posts/_form.html.erb+ to render a partial to make a tag:
<% end %>
</erb>
-This example shows another option of the render helper, being able to pass in local variables, in this case, we want the local variable +form+ in the partial to refer to the +post_form+ object.
+Note that we have changed the +f+ in +form_for(@post) do |f|+ to +post_form+ to make it easier to understand what is going on.
-You will also note that we also have changed the +f+ in <tt>form_for(@post) do |f|</tt> to <tt>post_form</tt> to clarify what is going on somewhat.
+This example shows another option of the render helper, being able to pass in local variables, in this case, we want the local variable +form+ in the partial to refer to the +post_form+ object.
We also add a <tt>@post.tags.build</tt> at the top of this form, this is to make sure there is a new tag ready to have it's name filled in by the user. If you do not build the new tag, then the form will not appear as there is no new Tag object ready to create.
@@ -1350,22 +1359,22 @@ Finally, we will edit the <tt>app/views/posts/show.html.erb</tt> template to sho
<p class="notice"><%= notice %></p>
<p>
- <strong>Name:</strong>
+ <b>Name:</b>
<%= @post.name %>
</p>
<p>
- <strong>Title:</strong>
+ <b>Title:</b>
<%= @post.title %>
</p>
<p>
- <strong>Content:</strong>
+ <b>Content:</b>
<%= @post.content %>
</p>
<p>
- <strong>Tags:</strong>
+ <b>Tags:</b>
<%= @post.tags.map { |t| t.name }.join(", ") %>
</p>
@@ -1405,22 +1414,22 @@ Now you can edit the view in <tt>app/views/posts/show.html.erb</tt> to look like
<p class="notice"><%= notice %></p>
<p>
- <strong>Name:</strong>
+ <b>Name:</b>
<%= @post.name %>
</p>
<p>
- <strong>Title:</strong>
+ <b>Title:</b>
<%= @post.title %>
</p>
<p>
- <strong>Content:</strong>
+ <b>Content:</b>
<%= @post.content %>
</p>
<p>
- <strong>Tags:</strong>
+ <b>Tags:</b>
<%= join_tags(@post) %>
</p>
@@ -1455,6 +1464,7 @@ h3. Changelog
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2
+* April 30, 2010: Fixes, editing and updating of code samples by "Rohit Arondekar":http://rohitarondekar.com
* April 25, 2010: Couple of more minor fixups "Mikel Lindsaar":credits:html#raasdnil
* April 1, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
* February 8, 2010: Full re-write for Rails 3.0-beta, added helpers and before_filters, refactored code by "Mikel Lindsaar":credits:html#raasdnil