Interior Night
Interior Night

Reputation: 1235

How do i apply a link to a td element in rails?

I have the following:

<td>
  <%= link_to simple_format(h(post.text)), post  %>
  <%= time_ago_in_words(post.created_at) %>
</td>

In rails, how do I make the entire td link to the post?

Upvotes: 4

Views: 3142

Answers (2)

Derrell Durrett
Derrell Durrett

Reputation: 576

I can report that I have used @Ribena's suggestion above based on @Rob Davis's answer and have created a div inside that makes (at least most of) the space in a <td> element.

<td> <%= link_to user_url user.id do %> <div id="clickable"> <%= user.name+' == '+user.current_score.to_s %> </div> <% end %> </td>

makes the bulk of the white space clickable.

Upvotes: 1

Rob Davis
Rob Davis

Reputation: 15802

If you want to make all of the text in the cell clickable, you could do this:

<td>
    <%= link_to post do %>
        <%= simple_format(h(post.text)) %>
        <%= time_ago_in_words(post.created_at) %>
    <% end %>
</td>

which would wrap an a tag around both of your text bits.

If you want to make the entire td background clickable, you can't just wrap the td with an a tag since HTML doesn't allow an a as a child of a tr, and behavior is likely to be inconsistent across browsers. Rails would gladly do it for you (just put the td inside the link_to block), but it probably wouldn't behave the way you want it to. You'd need different markup.

Upvotes: 5

Related Questions