【发布时间】:2021-08-14 20:05:56
【问题描述】:
我已经搜索了之前的问题,其中有几个问题与我的相似 - 但它们的解决方案对我不起作用。 Myapp 在本地运行良好,但在我尝试创建 cmets 时在 Heroku 上运行良好(作为用户,我正在使用设计)。我会尽力提供尽可能详细的信息。
我收到以下错误:
We're sorry, but something went wrong.
If you are the application owner check the logs for more information.
那我做
heroku 日志 --tail
我看到了这个
2021-05-26T14:34:26.408158+00:00 app[web.1]: I, [2021-05-26T14:34:26.408029 #4] INFO -- : [0aa9a88d-47f4-42b9-a93a-658d40d61fff] Started POST "/projects/1/comments" for 103.85.38.191 at 2021-05-26 14:34:26 +0000
2021-05-26T14:34:26.409593+00:00 app[web.1]: I, [2021-05-26T14:34:26.409489 #4] INFO -- : [0aa9a88d-47f4-42b9-a93a-658d40d61fff] Processing by CommentsController#create as HTML
2021-05-26T14:34:26.413951+00:00 app[web.1]: I, [2021-05-26T14:34:26.413812 #4] INFO -- : [0aa9a88d-47f4-42b9-a93a-658d40d61fff] Parameters: {"authenticity_token"=>"[FILTERED]", "comment"=>{"body"=>"hey"}, "commit"=>"Create Comment", "project_id"=>"1"}
2021-05-26T14:34:26.435342+00:00 app[web.1]: I, [2021-05-26T14:34:26.435230 #4] INFO -- : [0aa9a88d-47f4-42b9-a93a-658d40d61fff] Completed 500 Internal Server Error in 21ms (ActiveRecord: 4.0ms | Allocations: 1086)
2021-05-26T14:34:26.436572+00:00 app[web.1]: F, [2021-05-26T14:34:26.436494 #4] FATAL -- : [0aa9a88d-47f4-42b9-a93a-658d40d61fff]
2021-05-26T14:34:26.436575+00:00 app[web.1]: [0aa9a88d-47f4-42b9-a93a-658d40d61fff] ActiveModel::MissingAttributeError (can't write unknown attribute `user_id`):
2021-05-26T14:34:26.436576+00:00 app[web.1]: [0aa9a88d-47f4-42b9-a93a-658d40d61fff]
2021-05-26T14:34:26.436576+00:00 app[web.1]: [0aa9a88d-47f4-42b9-a93a-658d40d61fff] app/controllers/comments_controller.rb:5:in `create'
所以我尝试了其他问题推荐的类似问题,我做了
heroku rails/rake db 迁移
heroku 重启
heroku pg:重置数据库
它们都不起作用,或者对错误有任何影响。
作为参考,这里是错误引用的代码块(在 CommentsController 中)。
class CommentsController < ApplicationController
def create
@project = Project.find(params[:project_id])
@comment = @project.comments.create(comment_params)
@comment.user = current_user
@comment.save
redirect_to project_path(@project)
end
private
def comment_params
params.require(:comment).permit(:user_id, :body)
end
end
【问题讨论】:
-
关键部分是这个
ActiveModel::MissingAttributeError (can't write unknown attribute 'user_id'):。当时有current_user值吗?另外,你能分享一下comment模型吗? -
如果您的数据库中没有任何日期,您可以运行
db:schema:load而不是迁移,因为您的数据库中缺少一列。 -
我的评论模型只是
class Comment < ApplicationRecord belongs_to :project belongs_to :user end
标签: ruby-on-rails postgresql heroku