Sha256: 5da5ac4b7a80bc105ccc64b94c93a724134562d0067820cd889a871e2c94f2fe

Contents?: true

Size: 1.07 KB

Versions: 11

Compression:

Stored size: 1.07 KB

Contents

## View Helpers

View Helpers live in `app/helpers` and provide small snippets of reusable code for views. In our case, we want a method that strings a bunch of objects together using their name attribute and joining them with a comma. As this is for the Post show template, we put it in the `PostsHelper`.

Open up `app/helpers/posts_helper.rb` and add the following:

@@@ ruby
module PostsHelper
  def join_tags(post)
    post.tags.map { |t| t.name }.join(", ")
  end
end
@@@

Now you can edit the view in `app/views/posts/show.html.erb` to look like this:

@@@ html
<p class="notice"><%= notice %></p>

<p>
  <b>Name:</b>
  <%= @post.name %>
</p>

<p>
  <b>Title:</b>
  <%= @post.title %>
</p>

<p>
  <b>Content:</b>
  <%= @post.content %>
</p>

<p>
  <b>Tags:</b>
  <%= join_tags(@post) %>
</p>

<h2>Comments</h2>
<%= render :partial => "comments/comment",
           :collection => @post.comments %>

<h2>Add a comment:</h2>
<%= render "comments/form" %>


<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %> |
@@@

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
kitabu-1.0.6 examples/RailsGuides/text/11_View_helpers.mkdn
kitabu-1.0.5 examples/RailsGuides/text/11_View_helpers.mkdn
kitabu-1.0.4 examples/RailsGuides/text/11_View_helpers.mkdn
kitabu-1.0.3 examples/RailsGuides/text/11_View_helpers.mkdn
kitabu-1.0.2 examples/RailsGuides/text/11_View_helpers.mkdn
kitabu-1.0.1 examples/RailsGuides/text/11_View_helpers.mkdn
kitabu-1.0.0 examples/RailsGuides/text/11_View_helpers.mkdn
kitabu-1.0.0.rc4 examples/RailsGuides/text/11_View_helpers.mkdn
kitabu-1.0.0.rc3 examples/RailsGuides/text/11_View_helpers.mkdn
kitabu-1.0.0.rc2 examples/RailsGuides/text/11_View_helpers.mkdn
kitabu-1.0.0.rc1 examples/RailsGuides/text/11_View_helpers.mkdn