Ask LSBot

The link_to Helper

link_to

Let's now make adjustments for the index view template for posts.

<!-- app/views/posts/index.html.erb -->

<html>
  <body>
    <div class="posts">
      <% @posts.each do |post| %>
      <!-- use `@posts` instead of `posts` -->
        <div class="post">
          <h2 class="title">

            <%= link_to post.title, post_path(post.id) %>
          </h2>

          <small class="meta">
            <span class="author">by <%= post.author %> -</span>
            <em class="created_at"><%= post.created_at %></em>

            <%= link_to 'Edit', edit_post_path(post.id) %>

            <form method="post" action="/posts/<%= post.id %>" style='display: inline'>
              <input type="submit" value="Delete" />
            </form>

          </small>
        </div>
        <hr />
      <% end %>
    </div>

    <%= link_to 'New Post', new_post_path %>
  </body>
</html>

We have moved the view template to its conventional location, changed its name, utilized our instance variables from the controller, and we have also used the link helper link_to.

link_to works like this:

<%= link_to 'Link Text', url_or_path %>

We're also making use of our route-defined path helper methods in combination with link_to. For example, here we make the link to posts/show with:

<%= link_to post.title, post_path(post.id) %>

More About Helpers

link_to is a Rails built in helper that helps generate an anchor tag. To understand what it does and how it actually works, let's build a similar helper ourselves.

Helpers, or more specifically, view helpers are methods that generate HTML snippets to be placed in a view. To get a feel for what this means, let's take a look at a Rails-provided helper we've already seen:

<%= link_to 'Back to Posts', posts_path %>

<!-- this generates the HTML... -->

<a href="/posts">Back to Posts</a>

link_to is a helper method provided by Rails, but we can easily define a simple version of this helper ourselves. We'll name it my_link_to and place it in app/helpers/application_helper.rb - methods defined here will automatically become helpers and can be used in views.

### app/helpers/application_helper.rb ###

module ApplicationHelper
  def my_link_to(text, href)
    "<a href='#{href}'>#{text}</a>".html_safe
  end
end

Our my_link_to method is incredibly simple. It returns an <a> tag string, with the href and element contents (link text) set by the parameters. We also call html_safe on the string to prevent Rails from escaping this string.

So let's try using our my_link_to method in our posts/show view now:

<!-- app/views/posts/show.html.erb -->

<!-- ... -->

<div class="post">
  <!-- ... -->
</div>

<%= my_link_to 'Back to Posts', posts_path %>
<!-- generates... -->
<!-- <a href="/posts">Back to Posts</a> -->

This was a pretty simple helper method to start with, but through it we can clearly see that the purpose of helpers are to build HTML strings.

This conversation with LSBot is temporary. Sign up for free to save your conversations with LSBot.

Hi! I'm LSBot. I'm here to help you understand this chapter content with fast, focused answers.

Ask me about concepts, examples, or anything you'd like clarified from this chapter. I can explain complex topics, provide examples, or help connect ideas.

Want to know more? Refer to the LSBot User Guide.

This conversation with LSBot is temporary. Sign up for free to save your conversations with LSBot.

Hi! I'm LSBot. I'm here to help you think through this exercise by providing hints and guidance, without giving away the solution.

You can ask me about your approach, request clarification on the problem, or seek help when you feel stuck.

Want to know more? Refer to the LSBot User Guide.