简体   繁体   中英

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). 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

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 restart

heroku pg:reset DATABASE

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.

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

belongs_to :user, class_name: 'User'

but your comment table in the database doesn't have a user_id set. 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

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

It will return your comments table columns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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