1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

link_toの適用範囲を広げる

Last updated at Posted at 2021-11-05

修正前

     <table class="table table-hover offset-md-1">
        <% users.each do |user| %>
          <tbody>
            <tr>
              <td>
                <%= link_to  user_path(user) do %>
                  <%= attachment_image_tag(user, :profile_image, format: 'jpeg', fallback: "user_image.jpg", size:'50x50', class: "user-image") %>
                <% end %>
              </td>
            </tr>
          </tbody>
        <% end %>
      </table>

このままだと、attachment_image_tagの範囲しかlink_toが適用されない、、

修正後

     <table class="table table-hover offset-md-1">
        <% users.each do |user| %>
          <tbody>
            <tr onclick='window.location="<%= user_path(user) %>"' role="link">
              <td>
                <%= attachment_image_tag(user, :profile_image, format: 'jpeg', fallback: "user_image.jpg", size:'50x50', class: "user-image") %>
              </td>
              <td><%= user.name %></td>
            </tr>
          </tbody>
        <% end %>
      </table>

tr に JavaScript の windowオブジェクトを追加して遷移させることができる。

<tr onclick='window.location="<%= user_path(user) %>"' role="link">

問題点

画面遷移はできるがマウスオーバーした時にポインターにならない。

tr {
  cursor: pointer;
}

これで改善!

1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?