我似乎无法弄清楚为什么我一直收到这个错误。我正在尝试将评论表单添加到我正在制作的博客中,但它无法正常工作。以下是完整错误所说的内容。
帖子中的ActiveRecord :: StatementInvalid#show
显示/Users/ipbyrne/FirstRailsApp/blog/app/views/posts/show.html.erb第17行:
PG :: UndefinedColumn:错误:列comments.post_id不存在 第1行:SELECT COUNT()FROM" comments"在哪里"评论"。" post_id" ... ^ :SELECT COUNT()FROM" comments"在哪里"评论"。" post_id" = $ 1
</p>
<div id="comments">
<h2><%= @post.comments.count %> Comments</h2> <-- Says it breaks here
<%= render @post.comments %>
<h3>Add a comment:</h3>
根据我的理解,schema.rb文件中的一个表中缺少一列?这里的情况就像我的样子
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140911230918) do
create_table "comments", force: true do |t|
t.string "name"
t.text "body"
t.integer "post_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "comments", ["post_id"], name: "index_comments_on_post_id"
create_table "posts", force: true do |t|
t.string "title"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
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.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
答案 0 :(得分:4)
您可能只需要将列添加到comments
表中。
您可以通过终端创建迁移来添加列:
$ rails g migration add_post_id_column_to_comments post:belongs_to
$ rake db:migrate
您要使用post:belongs_to
的原因是Rails会自动附加_id
后缀并在注释表中创建外键以相互引用。
基本上,迁移post:belongs_to
的这一部分会将列post_id
添加到您的评论表中。 (例如,如果您执行cars:belongs_to
,则会得到cars_id
等)
通过这种方式,您可以参考帖子的评论:
@post.comments
您的@post.comments
现在失败的原因是它正在寻找您尚未制作的post_id
列,这可能也是因为您可能没有定义您的{{1}之间的关系1}}和Post
模型。
如果您还没有这样做,您只需要在每个模型中快速定义关系:
Comment
帖子。获取行话?
在belongs to
模型中,只需添加
Post
并在您的class Post < ActiveRecord::Base
has_many :comments # make sure it's pluralized
end
模型中添加
Comment
然后再次尝试运行您的应用。让我知道它是怎么回事。
答案 1 :(得分:1)
是的,您错过了post_id
模型上的字段Comment
,这会告诉Rails哪些评论属于哪些帖子。
您可以通过从命令行生成迁移来添加它,如下所示:
rails generate migration AddPostIdToComments post:references
rake db:migrate