9
6

More than 5 years have passed since last update.

railsでテーブル要素の一つ一つにリンクを貼る方法

Last updated at Posted at 2018-06-06

やりたいこと

テーブルの列を全部クリック可能にして、リンクで飛ばせるようにしたい。

trタグの間にaタグを置けないということで、
テーブルのへjsを仕込むやり方になりました。

リンク先は"/topics/1"など、それぞれのtopicのidが入ることを想定しております。


<tr onclick='window.location="<%= '/topics/' + topic.id.to_s %>"' role="link">

加えた後のコードは以下。

index.html.erb

  <tbody>
    <% @topics.each do |topic| %>
      <tr onclick='window.location="<%= '/topics/' + topic.id.to_s %>"' role="link">
        <td class="well"><%= link_to topic.content, topic %></td>
        <td><%= topic.like %></td>
      </tr>
    <% end %>
  </tbody>

困っていること

マウスオーバーした時にポインタがリンクっぽく変わらないのが微妙。

参考にしたリンク

以下の回答の2つ目を使用。
1つ目は戻ったのちに使えなくなるという問題があり。
https://stackoverflow.com/questions/17147821/how-to-make-a-whole-row-in-a-table-clickable-as-a-link

9
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6