【问题标题】:Rails App works locally, but not in production with HerokuRails 应用程序在本地工作,但不适用于 Heroku 的生产环境
【发布时间】: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 &lt; ApplicationRecord belongs_to :project belongs_to :user end

标签: ruby-on-rails postgresql heroku


【解决方案1】:

你的评论模型可能有belongs_to这样设置

belongs_to :user, class_name: 'User'

但是您在数据库中的评论表没有设置 user_id。也许您在本地数据库中手动插入了该列。如果是这样,请创建一个添加该列的迁移。

如果你想检查你的 cmets 表在 heroku 上的情况,请使用 heroku run rails c 在 heroku 上打开 rails 控制台并运行

ActiveRecord::Base.connection.columns('comments').map(&:name) # change 'comments' if your table has a different name

它将返回您的 cmets 表列

【讨论】:

  • 啊,我认为你是对的,在 Heroku 上,cmets 表没有 user_id(但这很奇怪,它在我的本地版本中有一个 - 它们不应该相同吗?)。同样奇怪的是我尝试使用add_reference :comments, :user, null: false, foreign_key: true 创建迁移,但它告诉我PG::DuplicateColumn: ERROR: column "user_id" of relation "comments" already exists
  • 也许你直接将它添加到你的数据库中,或者,创建了一个迁移,迁移然后删除了迁移文件。该错误是在您的本地服务器上还是在 Heroku 上弹出?
猜你喜欢
  • 2017-09-11
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 2013-11-13
  • 2011-06-12
  • 2016-01-14
  • 1970-01-01
相关资源
最近更新 更多