diff options
Diffstat (limited to 'railties/guides/source/action_view_overview.textile')
-rw-r--r-- | railties/guides/source/action_view_overview.textile | 86 |
1 files changed, 78 insertions, 8 deletions
diff --git a/railties/guides/source/action_view_overview.textile b/railties/guides/source/action_view_overview.textile index 5a1e8b1247..40cde6ad84 100644 --- a/railties/guides/source/action_view_overview.textile +++ b/railties/guides/source/action_view_overview.textile @@ -454,6 +454,83 @@ input("post", "title") # => <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /> </ruby> +h4. RecordTagHelper + +This module provides methods for generating a container tag, such as a +<div>+, for your record. This is the recommended way of creating a container for render your Active Record object, as it adds an appropriate class and id attributes to that container. You can then refer to those containers easily by following the convention, instead of having to think about which class or id attribute you should use. + +h5. content_tag_for + +Renders a container tag that relates to your Active Record Object. + +For example, given +@post+ is the object of +Post+ class, you can do: + +<ruby> +<%= content_tag_for(:tr, @post) do %> + <td><%= @post.title %></td> +<% end %> +</ruby> + +This will generate this HTML output: + +<html> +<tr id="post_1234" class="post"> + <td>Hello World!</td> +</tr> +</html> + +You can also supply HTML attributes as an additional option hash. For example: + +<ruby> +<%= content_tag_for(:tr, @post, :class => "frontpage") do %> + <td><%= @post.title %></td> +<% end %> +</ruby> + +Will generate this HTML output: + +<html> +<tr id="post_1234" class="post frontpage"> + <td>Hello World!</td> +</tr> +</html> + +You can pass a collection of Active Record objects. This method will loop through your objects and create a container for each of them. For example, given +@posts+ is an array of two +Post+ objects: + +<ruby> +<%= content_tag_for(:tr, @posts) do |post| %> + <td><%= post.title %></td> +<% end %> +</ruby> + +Will generate this HTML output: + +<html> +<tr id="post_1234" class="post"> + <td>Hello World!</td> +</tr> +<tr id="post_1235" class="post"> + <td>Ruby on Rails Rocks!</td> +</tr> +</html> + +h5. div_for + +This is actually a convenient method which calls +content_tag_for+ internally with +:div+ as the tag name. You can pass either an Active Record object or a collection of objects. For example: + +<ruby> +<%= div_for(@post, :class => "frontpage") do %> + <td><%= @post.title %></td> +<% end %> +</ruby> + +Will generate this HTML output: + +<html> +<div id="post_1234" class="post frontpage"> + <td>Hello World!</td> +</div> +</html> + h4. AssetTagHelper This module provides methods for generating HTML that links views to assets such as images, JavaScript files, stylesheets, and feeds. @@ -821,7 +898,7 @@ h5. select_year Returns a select tag with options for each of the five years on each side of the current, which is selected. The five year radius can be changed using the +:start_year+ and +:end_year+ keys in the +options+. <ruby> -# Generates a select field for five years on either side of +Date.today+ that defaults to the current year +# Generates a select field for five years on either side of Date.today that defaults to the current year select_year(Date.today) # Generates a select field from 1900 to 2009 that defaults to the current year @@ -1418,10 +1495,3 @@ end Then you could create special views like +app/views/posts/show.expert.html.erb+ that would only be displayed to expert users. You can read more about the Rails Internationalization (I18n) API "here":i18n.html. - -h3. Changelog - -* May 29, 2011: Removed references to remote_* helpers - Vijay Dev -* April 16, 2011: Added 'Using Action View with Rails', 'Templates' and 'Partials' sections. "Sebastian Martinez":http://wyeworks.com -* September 3, 2009: Continuing work by Trevor Turk, leveraging the Action Pack docs and "What's new in Edge Rails":http://ryandaigle.com/articles/2007/8/3/what-s-new-in-edge-rails-partials-get-layouts -* April 5, 2009: Starting work by Trevor Turk, leveraging Mike Gunderloy's docs |