gpt4 book ai didi

ruby-on-rails - ActiveModel::MissingAttributeError(无法写入未知属性 `user_id`)

转载 作者:行者123 更新时间:2023-12-03 20:45:38 26 4
gpt4 key购买 nike

部署我的应用程序时,我的评论有问题。在本地一切正常。来自 heroku 的日志说:

ActiveModel::MissingAttributeError (can't write unknown attribute `user_id`):
2018-01-02T15:52:43.461794+00:00 app[web.1]: [79cd7190-e2d9-4dd0-bf71-
709552e5c6e5] app/controllers/comments_controller.rb:15:in `create'

我不知道发生了什么错误。也许一些数据库的东西?

我的评论 Controller
class CommentsController < ApplicationController

def create
@post =Post.find(params[:post_id])
@comment =@post.comments.create(params[:comment].permit(:name, :body).merge(user: current_user))
redirect_to post_path(@post)
end


def destroy

@post = Post.find(params[:post_id])
@comment= @post.comments.find(params[:id])
if current_user.id == @comment.user_id
@comment.destroy
end
redirect_to post_path(@post)
end
end

我的模型
class User < ApplicationRecord
has_many :posts

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end

class Post < ApplicationRecord
belongs_to :user, optional: true
has_many :comments, dependent: :destroy

validates :title, presence: true, length: {minimum: 5}
validates :body, presence: true

end

class Comment < ApplicationRecord
belongs_to :post
belongs_to :user
end

我的迁移文件
class CreateComments < ActiveRecord::Migration[5.1]
def change
create_table :comments do |t|
t.string :name
t.text :body
t.references :post, index: true

t.timestamps
end
end
end

如果您需要更多代码或有任何想法,请告诉我

编辑:如果我添加一个 user_id 列,我会得到一个 SQLite3::SQLException: duplicate column name: user_id: ALTER TABLE "comments" ADD "user_id" integer错误

我的 schema.rb
`create_table "comments", force: :cascade do |t|
t.string "name"
t.text "body"
t.integer "post_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.index ["post_id"], name: "index_comments_on_post_id"
end

create_table "posts", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "theme"
t.integer "user_id"
end

create_table "users", force: :cascade 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", null: false
t.datetime "updated_at", 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

最佳答案

您需要添加 user_id列到您的 comments table 。 belongs_to需要这个。您还需要一个 post_id列和 user_id为您 posts表到。

您可以自定义列名,但约定是使用格式 parent_table_id .

这是关键引述,来自 docs :

Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model belongs_to another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model.



这意味着,例如,如果您的第一个用户的 ID 为 1 ,他们所有的评论和帖子都会有 user_id 1 的值,它实际将记录捆绑在一起。

这是包含相关行的示例迁移:
def change 
create_table :comments do |t|
...
t.belongs_to :user, index: true
...
end
end

那有意义吗?如果您有任何问题,请告诉我,我可以根据需要更新:)

关于ruby-on-rails - ActiveModel::MissingAttributeError(无法写入未知属性 `user_id`),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48064675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com