繁体   English   中英

Rails 应用程序在本地工作,但不适用于 Heroku 的生产环境

[英]Rails App works locally, but not in production with Heroku

我已经搜索了以前的问题,并且有几个与我的问题相似 - 但他们的解决方案对我不起作用。 Myapp 在本地运行良好,但在我尝试创建评论时(作为用户,我使用 devise)在 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 migrate

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

您的评论 model 可能有belongs_to这样设置

belongs_to :user, class_name: 'User'

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

如果您想检查您的评论表在 heroku 上的情况,请在 heroku 上使用heroku run rails c和 run rails Z4A8A08F09D37B73579

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

它将返回您的评论表列

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM