简体   繁体   English

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

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

I've searched through the previous questions and there were a couple that were similar to mine - but the solutions for them didn't work for me.我已经搜索了以前的问题,并且有几个与我的问题相似 - 但他们的解决方案对我不起作用。 Myapp works fine locally, but not on Heroku when I attempt to create comments (as a User, which I'm using devise for). Myapp 在本地运行良好,但在我尝试创建评论时(作为用户,我使用 devise)在 Heroku 上运行良好。 I'll try to give as much detail as I can.我会尽力提供尽可能多的细节。

I get the following error:我收到以下错误:

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

Then i do然后我做

heroku logs --tail heroku 日志 --tail

and I see this我看到了这个

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'

So I tried what the other questions recommended for similar issues, I did所以我尝试了其他问题推荐的类似问题,我做到了

heroku rails/rake db migrate heroku rails/rake db migrate

heroku restart heroku 重启

heroku pg:reset DATABASE heroku pg:重置数据库

None of them worked, or had any effect on the error.他们都没有工作,或对错误有任何影响。

For reference, here is the block of code (in CommentsController) that the error is referencing.作为参考,这里是错误引用的代码块(在 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

Your Comment model probably has belongs_to set like this您的评论 model 可能有belongs_to这样设置

belongs_to :user, class_name: 'User'

but your comment table in the database doesn't have a user_id set.但是您在数据库中的评论表没有设置 user_id。 Maybe you inserted the column manually on your local database.也许您在本地数据库中手动插入了该列。 If so, create a migration adding the column.如果是这样,请创建一个添加该列的迁移。

If you want to check how your comments table is on heroku, open rails console on heroku with heroku run rails c and run如果您想检查您的评论表在 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

It will return your comments table columns它将返回您的评论表列

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

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