0
0

More than 3 years have passed since last update.

画像を投稿するときに、undefined method `images' for nil:NilClassになってしまう問題を解決する

Last updated at Posted at 2021-03-08

今回、Active Storageを使って、画像を投稿する機能を作りました。
しかし、投稿自体はあるものの、投稿から投稿一覧に遷移するときに、タイトルのエラーになってしまうという問題にぶち当たりました。

結論から言うと、viewの引数の指定が間違っていたためです。なので、そこを修正してあげることで解決しました

以下、投稿一覧ページの、がぞうを表示する部分です

   <% if @post.images.attached? %>
         <div class = 'images'>
         <% @post.images.each do |image| %>
         <%= image_tag post.images %>
         </div>
         <% end %>
         <% end %>

こちらを間違えてしまってしました。

それを、こういった形に修正しました。

<% if post.images.attached? %>
    <div class = 'images'>
    <% post.images.each do |image| %>
    <%= image_tag image %>
    </div>
    <% end %>
    <% end %>

このように、インスタンス変数を指定していいたのですが、それらを取っ払い、image_tagの部分のimagesを、imageに変えました。

参考記事

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