2
2

More than 3 years have passed since last update.

Active Storageで、undefined method `image' for nil:NilClassになった

Last updated at Posted at 2021-03-23

Active Storageとdeviseを組み合わせてユーザー画像を表示できるものを作ろうとしたのですが、以下のようにエラーになりました

NoMethodError in Home#top
undefined method `image' for nil:NilClass

どうやらimageがユーザー画像を表示するところに入っていみたいです。

問題のソースコードは以下です

投稿一覧ページ

<div class="content-wrapper">
    <div class="content-block">
        <% @posts.each do |post| %>
        <div class="content">
         <div class="user-about">
             <div class="image">
             <% if @user.image.attached? %>
             <%= image_tag @user.image %>
                <% else %>
            <%= image_tag no.user.png %>
                <% end %>
             </div>

         <div class="profile">
            <div class="name-history">
                <div class="name">
                <%= user.name%>
                </div>
                <div class="mania-histry">
                    <%= "学習歴:#{user.mania_histry}年" %>
                </div>
             </div>

             <div class="enjoy-point">
             <%= "楽しいポイント#{user.enjoy_point}"%>
             </div>
         </div>
       </div>

       <div class="text">
        <p><%= post.content %></p>
    </div>

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

         <div class="action-menu">
             <div class="like">
             <h3>いいね件数: <%#= @post.likes.count %></h3>
             <%# if current_user.already_liked?(@post) %>
               <%#= button_to 'いいねを取り消す', post_like_path(@post), method: :delete %>
             <%# else %>
               <%#= button_to 'いいね', post_likes_path(@post) %>
             <%# end %>
             </div>
             <div class="comment">

             </div>
         </div>

        </div>
        <% end %>

    </div>
    <div class="sidebar">
     <div class="box">

     </div>
     <div class="box">

    </div>
    </div>
</div>

こちらのコードで
<%= image_tag @user.image %>となっている部分で上記のエラーになってしまっていました。
そこで色々試してみて、こちらのコードにすることで解消しました

解消したときのコード

<div class="content-wrapper">
    <div class="content-block">
        <% @posts.each do |post| %>
        <div class="content">
         <div class="user-about">
             <div class="image">
             <% if post.user.image.attached? %>
             <%= image_tag post.user.image %>
                <% else %>
            <%= image_tag no.user.png %>
                <% end %>
             </div>

         <div class="profile">
            <div class="name-history">
                <div class="name">
                <%= post.user.name%>
                </div>
                <div class="mania-histry">
                    <%= "学習歴:#{post.user.mania_histry}年" %>
                </div>
             </div>

             <div class="enjoy-point">
             <%= "楽しいポイント#{post.user.enjoy_point}"%>
             </div>
         </div>
       </div>

       <div class="text">
        <p><%= post.content %></p>
    </div>

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

         <div class="action-menu">
             <div class="like">
             <h3>いいね件数: <%#= @post.likes.count %></h3>
             <%# if current_user.already_liked?(@post) %>
               <%#= button_to 'いいねを取り消す', post_like_path(@post), method: :delete %>
             <%# else %>
               <%#= button_to 'いいね', post_likes_path(@post) %>
             <%# end %>
             </div>
             <div class="comment">

             </div>
         </div>

        </div>
        <% end %>

    </div>
    <div class="sidebar">
     <div class="box">

     </div>
     <div class="box">

    </div>
    </div>
</div>

7行目の<% if post.user.image.attached? %>の部分に注目していただきたいのですが、間違っている部分と比べると
<% if @user.image.attached? %>
<% if post.user.image.attached? %>となっています。

何故間違っていたのかの考察

each文の変数に@postが使われていて、それが使われている範囲内では、基本的にpostの値が繰り返し表示される。
その中で、@userという全く関係のないところのインスタンス変数が来てしまったからエラーになってしまったのではないかなと思っています

ですが、userとpostは外部キーで結ばれています。なので先頭にpostをつけることで、postと結びついたuserの情報をとってこれるようになったから解消できたのではないかなと思っています。

もし間違いなどありましたら指摘の方でコメントよろしくおねがいします

2
2
1

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