LoginSignup
0
0

More than 3 years have passed since last update.

投稿した画像にリンクをつける

Last updated at Posted at 2021-05-05

忘備録(自分用)

環境

  • Mac
  • ruby 2.6.5

解決したい内容

<%= link_to image-tag(item.image, class: "item-img" ),item_path(item) do %>の記述を追加すると、「NoMethodError in Items#index」を表示する。

投稿した画像をクリックすると、画像の詳細画面に遷移したい。

解決策

link_toの記載を見直す。

     <% @items.each do |item| %>
      <li class='list'>
        <%= link_to image-tag(item.image, class: "item-img" ),item_path(item) do %>
        <div class='item-img-content'>
        <% end %>
     <% end %>

上記を下記に修正

     <% @items.each do |item| %>
      <li class='list'>
        <%= link_to item_path(item) do %>
        <div class='item-img-content'>
          <%= image_tag item.image, class: "item-img" %>
        <% end %>
     <% end %>

link_to とimage_tagを一緒にせず、do〜endの中に分けて記述することで、解決。
一緒に記述する方法もありそうだが、現時点では、エラーが出るため、上記にて対応。

参考にした記事

https://ja.stackoverflow.com/questions/30523/image-tag%E3%81%A8link-to%E3%81%AE%E7%B5%84%E3%81%BF%E5%90%88%E3%82%8F%E3%81%9B%E3%81%A7%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%8C%E8%B5%B7%E3%81%8D%E3%81%BE%E3%81%99)

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