Rails应用程序可以在本地运行,但不能在生产环境中与Heroku一起运行

i34xakig  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(154)

我已经搜索了之前的问题,有几个和我的问题相似--但是他们的解决方案对我不起作用。我的应用程序在本地运行良好,但是当我试图创建评论时(作为一个用户,我正在使用design),在Heroku上就不行了。我会尽可能多地给予细节。
出现以下错误:

We're sorry, but something went wrong.
If you are the application owner check the logs for more information.

那我就做了
大鹿原木--尾部
我看到了这个

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数据库迁移
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
ozxc1zmp

ozxc1zmp1#

您的注解模型可能具有如下设置的belongs_to

belongs_to :user, class_name: 'User'

但是数据库中的注解表没有设置user_id。可能是您在本地数据库中手动插入了该列。如果是这样,请创建一个添加该列的迁移。
如果要检查heroku上的注解表的情况,请使用heroku run rails c打开heroku上的rails控制台,然后运行

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

它将返回您的注解表列

相关问题