在Rails中使用link_to和post.title?

时间:2012-02-23 17:02:07

标签: ruby-on-rails

我有以下代码:

  <% @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>

我想有这样的事情:

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

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:1)

如果你想用var添加文本,你也可以<h2><%= link_to "Title : #{post.title}", post %></h2>