Search code examples
rubyruby-on-rails-4ruby-on-rails-5

Can't access user in comment object


I have the following models, Post, Comment, and User. The Comment object belongs to user, and the user has many comments. However, when trying to render a view to show the comments, the user can't be accessed and shows undefined method `avatar' for nil:NilClass. The avatar attached is a method associated with the User class.

This is the comments controller

class CommentsController < ApplicationController

    before_action :authenticate_user!, except: [:index, :show]
    before_action :set_commentable 

    def create
        @comment = @commentable.comments.new comment_params
        @comment.user = current_user
        @comment.save
        redirect_to @commentable, notice: "Comment Posted!"
    end

    def index
        @comments = @commentable.comments.all
        @comment = @commentable.comments.new comment_params
    end



    private

        def comment_params
            params.require(:comment).permit(:body)
        end

        def set_commentable
            @commentable = Post.find(params[:post_id])
        end

end

Here is the Comment model:

class Comment < ApplicationRecord
    belongs_to :user
    belongs_to :commentable, polymorphic: true
end

Here is the User model:

class User < ApplicationRecord

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  acts_as_voter

  has_many :posts
  has_many :comments
  has_one_attached :avatar


  validates :username, uniqueness: true
  validates :email, uniqueness: true

end

Here is the view I am trying to render. I want the comments to be displayed at the bottom of the show view for a post:

<p id="notice"><%= notice %></p>

<div class="w3-container w3-card w3-white w3-round m7" style="margin:auto; display:table; min-width: 500px; max-width: 800px;"><br>
        <% if @post.user.avatar.attached? %>
            <%= image_tag @post.user.avatar, class: "w3-circle avatar w3-left w3-margin-right", :style => "display: inline;" %>
        <% else %>
        <img src="https://www.w3schools.com/w3images/avatar2.png" alt="Avatar" class="w3-left w3-circle w3-margin-right" style="width:60px">
        <% end %>
        <%= link_to @post do %>
        <span class="w3-right w3-opacity"><%= time_ago_in_words(@post.created_at) %> ago</span>
        <% end %>

        <h4><%= @post.user.name %></h4> <h5>@<%= @post.user.username%></h5>
        <hr class="w3-clear">
        <p><%= @post.content %></p>
        <% if user_signed_in? %>
        <hr class="w3-clear">
        <%= link_to like_post_path(@post), method: :put,  class: "w3-button w3-theme-d2 w3-margin-bottom" do%>
            <i class="fa fa-thumbs-up"></i>
            <span class="w3-badgeM"><%= @post.get_upvotes.size %></span>
        <% end %>    
        <button type="button" class="w3-button w3-theme-d2 w3-margin-bottom"><i class="fa fa-comment"></i></button>

        <% if current_user.id == @post.user_id %>
            <%= link_to edit_post_path(@post), class: "w3-button w3-theme-d1 w3-margin-bottom" do %>
                <i class="fas fa-pencil-alt"></i>
            <% end %>
            <%= link_to @post, method: :delete, data: {confirm: "Are you sure you want to delete this post?"}, class: "w3-button w3-theme-d1 w3-margin-bottom" do %>
                <i class="fas fa-trash-alt"></i>
            <% end %>
        <% end %>
        <% else %>
        <br >
        <% end %>

        <%= simple_form_for([@post, @post.comments.build]) do |f| %>
        <div class="field">
            <div class="control">
            <%= f.input :body, input_html: { class: 'textarea' }, wrapper: false, label_html: { class: 'label' }  %>
            </div>
        </div>
        <%= f.button :submit, 'Leave a reply', class: "button is-primary" %>
        <% end %>
        <% @post.comments.each do |comment|%>
        <div class="w3-container w3-card w3-round w3-margin" style="background-color: #f3f3f3; padding: 0px 5px 5px;" ><br>
            <% if comment.user.avatar.attached? %>
                <%= image_tag comment.user.avatar, class: "w3-circle avatar w3-left w3-margin-right", :style => "display: inline;" %>
            <% else %>
                <img src="https://www.w3schools.com/w3images/avatar2.png" alt="Avatar" class="w3-left w3-circle w3-margin-right" style="width:60px">
            <% end %>
            <% if user_signed_in? %>

            <% end %>
            <span class="w3-right w3-opacity"><%= time_ago_in_words(comment.created_at) %> ago</span>
            <h6><%= comment.user.name %></h6> <p><strong>@<%= comment.user.username%></strong></p>
            <p style="max-height: 75px; overflow:auto;"><%= comment.body %></p>

        </div>
        <% end %>

    </div>

The routes.rb file:

Rails.application.routes.draw do
  devise_for :users
  #  :controllers => {registrations: 'registrations'}
  resources :posts do
    member do
      put "like" => "posts#upvote"
      put "unlike" => "posts#downvote"
    end
    resources :comments
  end   
  root "posts#index"
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

However, when I try to render this, I get the following error:

https://i.ibb.co/HV3qkr7/error.jpg

If I look in the database, the comment record exists with the correct ids:

https://i.ibb.co/FXvPpSF/db.jpg

When I try to run the avatar attached method in the console after selecting the post, it seems to return true and not throw any errors:

https://i.ibb.co/X3k3mBq/console.jpg

Could someone help me out, and point out where I went wrong? Thanks :)


Solution

  • It was actually a simple fix! I just had to wrap the comments view inside <% if !comment.user.nil? %> For some reason, the first comment does not seem to have a user associated with it.