DEV Community

Saral Karki
Saral Karki

Posted on

First issue of this kind

This was inevitable, my app seems to be working well in my localhost, but when deployed to heroku, it has fallen apart. I write this post after hours of trying to fix it. I have partially fixed it, however, another issue persists. I will take a look at it tomorrow with a fresh head. It has been a long day, frustrating at times, but as I reflect back on my day, it has been well worth the effort.

2019-02-06T15:45:57.801026+00:00 heroku[router]: at=info method=GET path="/sessions/1" host=threeirblog.herokuapp.com request_id=32d62fd7-3d03-4022-9e89-9333e485c08a fwd="202.51.93.108" dyno=web.1 connect=0ms service=12ms status=200 bytes=2693 protocol=https
2019-02-06T15:46:03.175402+00:00 heroku[router]: at=info method=GET path="/posts/new" host=threeirblog.herokuapp.com request_id=bf9f0490-5bb3-4436-baef-3f3c7c998249 fwd="202.51.93.108" dyno=web.1 connect=0ms service=7ms status=500 bytes=1827 protocol=https
2019-02-06T15:46:03.168691+00:00 app[web.1]: I, [2019-02-06T15:46:03.168598 #4]  INFO -- : [bf9f0490-5bb3-4436-baef-3f3c7c998249] Started GET "/posts/new" for 202.51.93.108 at 2019-02-06 15:46:03 +0000
2019-02-06T15:46:03.169363+00:00 app[web.1]: I, [2019-02-06T15:46:03.169307 #4]  INFO -- : [bf9f0490-5bb3-4436-baef-3f3c7c998249] Processing by PostsController#new as HTML
2019-02-06T15:46:03.172576+00:00 app[web.1]: D, [2019-02-06T15:46:03.172510 #4] DEBUG -- : [bf9f0490-5bb3-4436-baef-3f3c7c998249]   [1m[36mUser Load (1.2ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2[0m  [["id", 1], ["LIMIT", 1]]
2019-02-06T15:46:03.173490+00:00 app[web.1]: I, [2019-02-06T15:46:03.173429 #4]  INFO -- : [bf9f0490-5bb3-4436-baef-3f3c7c998249] Completed 500 Internal Server Error in 4ms (ActiveRecord: 1.2ms)
2019-02-06T15:46:03.173902+00:00 app[web.1]: F, [2019-02-06T15:46:03.173845 #4] FATAL -- : [bf9f0490-5bb3-4436-baef-3f3c7c998249]   
2019-02-06T15:46:03.173976+00:00 app[web.1]: F, [2019-02-06T15:46:03.173903 #4] FATAL -- : [bf9f0490-5bb3-4436-baef-3f3c7c998249] ActiveModel::UnknownAttributeError (unknown attribute 'user_id' for Post.):
2019-02-06T15:46:03.174011+00:00 app[web.1]: F, [2019-02-06T15:46:03.173967 #4] FATAL -- : [bf9f0490-5bb3-4436-baef-3f3c7c998249]   
2019-02-06T15:46:03.174054+00:00 app[web.1]: F, [2019-02-06T15:46:03.174015 #4] FATAL -- : [bf9f0490-5bb3-4436-baef-3f3c7c998249] app/controllers/posts_controller.rb:22:in `new'


The error as seen on my heroku console log. Any suggestion so as to what I should be looking for. I can see a fatal, unknown attribute 'user_id' for Post. But, these very code work fine on my localhost. Is it a case of my Post database not getting migrated. I have run db:migrate multiple times, and I really don't know how to go about it any more. Any suggestions, on how I can debug better and find what lines are causing the issue?

What my posts_controller.rb looks like? I can see from the console it points me to the new action in line 22.

lass PostsController < ApplicationController
    skip_before_action :require_valid_user!, only: [:index]



    def index
        @post = Post.all
        if session[:user_id] 
            @user_id = current_user.post.all
        end


    end

    def show
        @post = Post.find(params[:id])
    end

    def new
        @post = current_user.post.build       
    end

    def create
        post = current_user.post.build(post_params)
        if post.save
            redirect_to post_path(post)           
        else
            flash[:error] =   "could not save"
            redirect_to new_post_path          
        end
    end

    def edit
        @post = Post.find(params[:id])
    end

    def update
        @post = Post.find(params[:id])
        if @post.update(post_params)
            redirect_to @post
          else
            render 'edit'
        end
    end

    def destroy
        @post = Post.find(params[:id])
        @post.destroy
            redirect_to root_path
    end


    private 
        def post_params
            params.require(:post).permit( :title , :blob)
        end



end


Prior to this, I was having an issue with the User model, I sorted that out by removing the user model and running db:migrate again. That seemed to do that trick for the user and I was able to register and login. However, now the issue seems to be on the post model.

Anyway, time for me to sleep on it , and get back at it tomorrow.

The app

Top comments (0)