wyc
wyc

Reputation: 55323

Using link_to with post.title in Rails?

I have the following code:

  <% @posts.each do |post| %>
    <h2>Title: <%= post.title %></h2>
    <p>Author: <%= post.user.username %></p>
    <p>Created At: <%= post.created_at %></p>
    <p>Content: <%= post.content %></p>
    <p>Votes: <%= post.total_votes %></p>
    <p>Comments: <%= post.comments_count %></p>
    <ul>
      <li><%= link_to 'Show', post %></li>
      <li><%= link_to 'Edit', edit_post_path(post) %></li>
      <li><%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %></li>
    </ul>

I would like to have something like this:

<h2><%= link_to "post.title" %></h2>

What's the right way of doing this?

Upvotes: 0

Views: 935

Answers (2)

ScottJShea
ScottJShea

Reputation: 7111

If you want to add text with the var you can do <h2><%= link_to "Title : #{post.title}", post %></h2> too.

Upvotes: 1

James
James

Reputation: 4807

<h2><%= link_to post.title, post %></h2>

Upvotes: 10

Related Questions