skip to Main Content

I’m using Twitter Bootstrap and I want to turn the following list-group-item into a link_to, i.e. link_to (post.title, post_path) but the following link contains a h4 and p element. How would I incorporate this? I though of just turning it all into a string, but then would you not have a problem with escaping html?

<% @posts.each do |post| %>
  <a href="#" class="list-group-item">
    <h4 class="list-group-item-heading"><%= post.title %></h4>
    <p class="list-group-item-text"><%= post.content %></p>
  </a>
<% end %>

2

Answers


  1. link_to accepts a block

    <%= link_to post_path(post) do %>
     <h4 class="list-group-item-heading"><%= post.title %></h4>
     <p class="list-group-item-text"><%= post.content %></p>
    <% end %>
    

    If you want any other options you’d put them on the end but before the block

    <%= link_to post_path(post), remote: true do %>
    
    Login or Signup to reply.
  2. Here you are:

    <% @posts.each do |post| %>
      <%= link_to post_path(post) do %>
        <h4 class="list-group-item-heading"><%= post.title %></h4>
        <p class="list-group-item-text"><%= post.content %></p>
      <% end %>
    <% end %>
    

    To learn more about link_to: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search