首页 > 解决方案 > 我无法在视图部分显示评论的创建者

问题描述

我正在做一个 facebook 克隆应用程序,我正在帖子中实现评论。我可以显示帖子中的评论,但是当我尝试输入评论创建者的姓名时,它会显示一个错误。

我得到的错误

这就是我现在所拥有的。

图式

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

用户显示视图

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

用户控制器

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

评论控制器

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

用户模型

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

后模型

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

  has_many :likes

  has_many :comments
end

评论模型

class Comment < ApplicationRecord
  validates :body, presence: true

  belongs_to :user
  belongs_to :post
end

标签: ruby-on-railsassociations

解决方案


看起来您有一条未附加到用户的评论。这可能只是闲置的测试数据,但您应该满足这一点,并且仅在有用户时才显示用户

<%= comment.user.name %>

可能成为

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

您还应该迎合没有名字的场合。

无论如何,如果您添加该修复程序,您将能够更轻松地判断哪些记录不是您所期望的,并对它们进行适当的排序

这样的事情可能会有所帮助

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

代码未经测试,所以如果您有问题,请告诉我您可以检查空白吗?若你宁可

更新

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

推荐阅读