NaguiHW
NaguiHW

Reputation: 151

I can't show the creator of a comment in the view part

I am doing a facebook clone app and I am implementing comments in posts. I can show the comments from the post but when I try to put the name of the creator of the comment, it show me an error.

The error that I get

This is what I have right now.

schema

ActiveRecord::Schema.define(version: 2019_09_02_233213) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "comments", force: :cascade do |t|
    t.text "body"
    t.bigint "user_id", null: false
    t.bigint "post_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["post_id"], name: "index_comments_on_post_id"
    t.index ["user_id"], name: "index_comments_on_user_id"
  end

  create_table "likes", force: :cascade do |t|
    t.bigint "user_id", null: false
    t.bigint "post_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["post_id"], name: "index_likes_on_post_id"
    t.index ["user_id"], name: "index_likes_on_user_id"
  end

  create_table "posts", force: :cascade do |t|
    t.bigint "user_id", null: false
    t.text "body", default: "", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["user_id"], name: "index_posts_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "name", default: "", null: false
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "comments", "posts"
  add_foreign_key "comments", "users"
  add_foreign_key "likes", "posts"
  add_foreign_key "likes", "users"
  add_foreign_key "posts", "users"
end

users show views

<h1><%= @user.name %></h1>

<% @posts.each do |post| %>
    <%= post.body %><br>
    <%= form_for(post.comments.build, url: post_comments_path(post)) do |f| %>
        <%= f.text_area :body %><br>
        <%= f.submit 'Comment' %><br>
    <% end %>
    <% post.comments.each do |comment| %>
        <%= comment.user.name %>
        <%= comment.body %><br>
    <% end %>
<% end %>

users controllers

class UsersController < ApplicationController
    before_action :authenticate_user!

    def index
        @users = User.all
    end

    def show
        @user = User.find(params[:id])
        @posts = @user.posts
    end
end

comment controllers

class CommentsController < ApplicationController
    def create
        post = Post.find(params[:post_id])
        comment = post.comments.build(comment_params.merge(user: current_user))
        if comment.save
            redirect_back(fallback_location: root_path)
        else
            flash[:error] = comment.errors.full_messages
            redirect_back(fallback_location: root_path)
        end
    end

    def destroy

    end

    private

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

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

  has_many :posts

  has_many :likes

  has_many :comments
end

post model

class Post < ApplicationRecord
  validates :body, presence: true
  belongs_to :user

  has_many :likes

  has_many :comments
end

comment model

class Comment < ApplicationRecord
  validates :body, presence: true

  belongs_to :user
  belongs_to :post
end

Upvotes: 1

Views: 36

Answers (1)

jamesc
jamesc

Reputation: 12837

It looks like you have a comment that is not attached to a user. This could just be test data that has been left hanging around but you should cater for this and only display the user if there is one

<%= comment.user.name %>

Could become

<%= comment.user.name unless comment.user.blank? %>

You should also cater for the occasion when a name is not present.

Anyway, if you add that fix you will be able to tell more easily which records are not what you expect them to be and sort them appropriately

Something like this might help

<% if comment.user %>
    <%= comment.user.name unless comment.user.name.blank? %>
<%else%>
  <p> The comment <%= comment.body %> has no user attached. Please fix this. The comment ID is: <%=comment.id%></p>
<%end%>

Code is untested so if you have a problem let me know You can check for blank? if you prefer

UPDATE

<% post.comments.each do |comment| %>
  <% if comment.user %>
    <%= comment.user.name %>
    <%= comment.body %><br>
  <%else%>
    No user for <%= comment.body %>
  <%end%>
<% end %>

Upvotes: 1

Related Questions