From 396d599e24cbfa15c62b20fab8cc02cdba046ce3 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Thu, 6 Nov 2008 01:10:30 +0530 Subject: Update guides from docrails --- .../guides/html/getting_started_with_rails.html | 114 +++++++++++++-------- 1 file changed, 69 insertions(+), 45 deletions(-) (limited to 'railties/doc/guides/html/getting_started_with_rails.html') diff --git a/railties/doc/guides/html/getting_started_with_rails.html b/railties/doc/guides/html/getting_started_with_rails.html index 797ae2fb3a..1b2eac0ce5 100644 --- a/railties/doc/guides/html/getting_started_with_rails.html +++ b/railties/doc/guides/html/getting_started_with_rails.html @@ -1457,51 +1457,57 @@ http://www.gnu.org/software/src-highlite -->

At this point, it’s worth looking at some of the tools that Rails provides to eliminate duplication in your code. In particular, you can use partials to clean up duplication in views and filters to help with duplication in controllers.

7.1. Using Partials to Eliminate View Duplication

As you saw earlier, the scaffold-generated views for the new and edit actions are largely identical. You can pull the shared code out into a partial template. This requires editing the new and edit views, and adding a new template:

-

new.html.erb: -[source, ruby]

+

new.html.erb:

-
-
<h1>New post</h1>
+
+
<h1>New post</h1>
 
-<%= render :partial => "form" %>
+<%= render :partial => "form" %>
 
-<%= link_to 'Back', posts_path %>
-
-

edit.html.erb: -[source, ruby]

+<%= link_to 'Back', posts_path %> +
+

edit.html.erb:

-
-
<h1>Editing post</h1>
+
+
<h1>Editing post</h1>
 
-<%= render :partial => "form" %>
+<%= render :partial => "form" %>
 
-<%= link_to 'Show', @post %> |
-<%= link_to 'Back', posts_path %>
-
-

_form.html.erb: -[source, ruby]

+<%= link_to 'Show', @post %> | +<%= link_to 'Back', posts_path %> +
+

_form.html.erb:

-
-
<% form_for(@post) do |f| %>
-  <%= f.error_messages %>
-
-  <p>
-    <%= f.label :name %><br />
-    <%= f.text_field :name %>
-  </p>
-  <p>
-    <%= f.label :title, "title" %><br />
-    <%= f.text_field :title %>
-  </p>
-  <p>
-    <%= f.label :content %><br />
-    <%= f.text_area :content %>
-  </p>
-  <p>
-    <%= f.submit "Save" %>
-  </p>
-<% end %>
-
+
+
<% form_for(@post) do |f| %>
+  <%= f.error_messages %>
+
+  <p>
+    <%= f.label :name %><br />
+    <%= f.text_field :name %>
+  </p>
+  <p>
+    <%= f.label :title, "title" %><br />
+    <%= f.text_field :title %>
+  </p>
+  <p>
+    <%= f.label :content %><br />
+    <%= f.text_area :content %>
+  </p>
+  <p>
+    <%= f.submit "Save" %>
+  </p>
+<% end %>
+

Now, when Rails renders the new or edit view, it will insert the _form partial at the indicated point. Note the naming convention for partials: if you refer to a partial named form inside of a view, the corresponding file is _form.html.erb, with a leading underscore.

For more information on partials, refer to the Layouts and Rending in Rails guide.

7.2. Using Filters to Eliminate Controller Duplication

@@ -1721,32 +1727,32 @@ http://www.gnu.org/software/src-highlite -->
  • -+app/helpers/comments_helper.rb - A view helper file +app/helpers/comments_helper.rb - A view helper file

  • -+app/views/comments/index.html.erb - The view for the index action +app/views/comments/index.html.erb - The view for the index action

  • -+app/views/comments/show.html.erb - The view for the show action +app/views/comments/show.html.erb - The view for the show action

  • -+app/views/comments/new.html.erb - The view for the new action +app/views/comments/new.html.erb - The view for the new action

  • -+app/views/comments/edit.html.erb - The view for the edit action +app/views/comments/edit.html.erb - The view for the edit action

  • -+test/functional/comments_controller_test.rb - The functional tests for the controller +test/functional/comments_controller_test.rb - The functional tests for the controller

  • @@ -1984,7 +1990,7 @@ http://www.gnu.org/software/src-highlite -->
    +

    Rails also comes with built-in help that you can generate using the rake command-line utility:

    +

    10. Changelog

    @@ -2010,6 +2029,11 @@ The Rails wiki
    • +November 3, 2008: Formatting patch from Dave Rothlisberger +

      +
    • +
    • +

      November 1, 2008: First approved version by Mike Gunderloy

    • -- cgit v1.2.3