From b84cee08c6129717d7c3291918f06f95cc9eb916 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 2 Sep 2011 21:35:24 +0700 Subject: Make `content_tag_for` and `div_for` accepts the array of records So instead of having to do this: @items.each do |item| content_tag_for(:li, item) do Title: <%= item.title %> end end You can now do this: content_tag_for(:li, @items) do |item| Title: <%= item.title %> end --- .../guides/source/action_view_overview.textile | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'railties/guides/source') diff --git a/railties/guides/source/action_view_overview.textile b/railties/guides/source/action_view_overview.textile index 5a1e8b1247..d1827c649b 100644 --- a/railties/guides/source/action_view_overview.textile +++ b/railties/guides/source/action_view_overview.textile @@ -454,6 +454,83 @@ input("post", "title") # => +h4. RecordTagHelper + +This module provides methods for generating a container tag, such as a +
+, 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: + + +<%= content_tag_for(:tr, @post) do %> + <%= @post.title %> +<% end %> + + +This will generate this HTML output: + + + + Hello World! + + + +You can also supply HTML attributes as an additional option hash. For example: + + +<%= content_tag_for(:tr, @post, :class => "frontpage") do %> + <%= @post.title %> +<% end %> + + +Will generate this HTML output: + + + + Hello World! + + + +You can pass a collection of Active Record objects. This method will loops through your objects and create a container for each of them. For example, given +@posts+ is an array of two +Post+ objects: + + +<%= content_tag_for(:tr, @posts) do |post| %> + <%= post.title %> +<% end %> + + +Will generate this HTML output: + + + + Hello World! + + + Ruby on Rails Rocks! + + + +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: + + +<%= div_for(@post, :class => "frontpage") do %> + <%= @post.title %> +<% end %> + + +Will generate this HTML output: + + +
+ Hello World! +
+ + h4. AssetTagHelper This module provides methods for generating HTML that links views to assets such as images, JavaScript files, stylesheets, and feeds. -- cgit v1.2.3