Can't get resources to work on rails on Heroku - ruby-on-rails

I'm a rails newbie having trouble getting resources to work on rails when deployed on Heroku. It works in my development environment (Cloud9), but not on Heroku. My routes.rb is as follows:
Rails.application.routes.draw do
root 'pages#home'
get '/home', to: 'pages#home'
resources :characters
end
When I run it on Cloud9, it works as it should and rake routes returns:
Prefix Verb URI Pattern Controller#Action
root GET / pages#home
home GET /home(.:format) pages#home
GET /home(.:format) pages#home
characters GET /characters(.:format) characters#index
POST /characters(.:format) characters#create
new_character GET /characters/new(.:format) characters#new
edit_character GET /characters/:id/edit(.:format) characters#edit
character GET /characters/:id(.:format) characters#show
PATCH /characters/:id(.:format) characters#update
PUT /characters/:id(.:format) characters#update
DELETE /characters/:id(.:format) characters#destroy
but on Heroku, I get an error message saying We're sorry, but something went wrong. If you are the application owner check the logs for more information. and the logs show me this:
2018-01-10T21:58:43.431137+00:00 app[web.1]: Completed 500 Internal Server Error in 6ms (ActiveRecord: 4.1ms)
2018-01-10T21:58:43.433165+00:00 app[web.1]: FROM pg_attribute a LEFT JOIN pg_attrdef d
2018-01-10T21:58:43.433157+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "characters" does not exist
2018-01-10T21:58:43.433168+00:00 app[web.1]: WHERE a.attrelid = '"characters"'::regclass
2018-01-10T21:58:43.433154+00:00 app[web.1]:
2018-01-10T21:58:43.433166+00:00 app[web.1]: ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2018-01-10T21:58:43.433159+00:00 app[web.1]: ^
2018-01-10T21:58:43.433174+00:00 app[web.1]:
2018-01-10T21:58:43.433169+00:00 app[web.1]: ORDER BY a.attnum
2018-01-10T21:58:43.425295+00:00 app[web.1]: Processing by CharactersController#new as HTML
2018-01-10T21:58:43.433172+00:00 app[web.1]: app/controllers/characters_controller.rb:7:in `new'
2018-01-10T21:58:43.433158+00:00 app[web.1]: LINE 5: WHERE a.attrelid = '"characters"'::regclass
2018-01-10T21:58:43.433170+00:00 app[web.1]: ):
2018-01-10T21:58:43.422364+00:00 app[web.1]: Started GET "/characters/new" for 24.155.28.170 at 2018-01-10 21:58:43 +0000
2018-01-10T21:58:43.433168+00:00 app[web.1]: AND a.attnum > 0 AND NOT a.attisdropped
2018-01-10T21:58:43.433161+00:00 app[web.1]: : SELECT a.attname, format_type(a.atttypid, a.atttypmod),
2018-01-10T21:58:43.433164+00:00 app[web.1]: pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
2018-01-10T21:58:43.433173+00:00 app[web.1]:
2018-01-10T21:58:58.637394+00:00 app[web.1]: pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
2018-01-10T21:58:58.637395+00:00 app[web.1]: FROM pg_attribute a LEFT JOIN pg_attrdef d
2018-01-10T21:58:58.626386+00:00 app[web.1]: Started GET "/characters/new" for 24.155.28.170 at 2018-01-10 21:58:58 +0000
2018-01-10T21:58:58.637397+00:00 app[web.1]: WHERE a.attrelid = '"characters"'::regclass
2018-01-10T21:58:58.637402+00:00 app[web.1]:
2018-01-10T21:58:58.637392+00:00 app[web.1]: : SELECT a.attname, format_type(a.atttypid, a.atttypmod),
2018-01-10T21:58:58.637389+00:00 app[web.1]: LINE 5: WHERE a.attrelid = '"characters"'::regclass
2018-01-10T21:58:58.637400+00:00 app[web.1]: ):
2018-01-10T21:58:58.637401+00:00 app[web.1]: app/controllers/characters_controller.rb:7:in `new'
2018-01-10T21:58:58.635689+00:00 app[web.1]: Completed 500 Internal Server Error in 6ms (ActiveRecord: 4.0ms)
2018-01-10T21:58:58.637399+00:00 app[web.1]: ORDER BY a.attnum
2018-01-10T21:58:58.637385+00:00 app[web.1]:
2018-01-10T21:58:58.637391+00:00 app[web.1]: ^
2018-01-10T21:58:58.637396+00:00 app[web.1]: ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2018-01-10T21:58:58.637398+00:00 app[web.1]: AND a.attnum > 0 AND NOT a.attisdropped
2018-01-10T21:58:58.629873+00:00 app[web.1]: Processing by CharactersController#new as HTML
2018-01-10T21:58:58.637388+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "characters" does not exist
2018-01-10T21:58:58.637403+00:00 app[web.1]:
What's wrong and how do I fix it?

Assuming you haven't done anything absolutely with where you're putting your mdoels, ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "characters" does not exist is indicative of not having the table inside of PostgreSQL.
heroku run rake db:migrate should do the trick.

Related

Connecting Postgresql to Rails in Heroku

So I recently deployed my Rails backend server to Heroku.
After a little struggling with passing from SQLite to PostgreSQL, I finally got everything running.
Now the problem is, I did rake db:create and rake db:migrate and that went without any errors but now when I make a REST call to the server I have this error (complete) :
2019-07-31T19:42:16.534355+00:00 app[web.1]: I, [2019-07-31T19:42:16.534259 #4] INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] Started POST "/sessions" for 91.176.204.35 at 2019-07-31 19:42:16 +0000
2019-07-31T19:42:16.535824+00:00 app[web.1]: I, [2019-07-31T19:42:16.535063 #4] INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] Processing by SessionsController#create as */*
2019-07-31T19:42:16.535947+00:00 app[web.1]: I, [2019-07-31T19:42:16.535882 #4] INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] Parameters: {"email"=>"titivermeesch#gmail.com", "password"=>"[FILTERED]"}
2019-07-31T19:42:16.566455+00:00 app[web.1]: I, [2019-07-31T19:42:16.566340 #4] INFO -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] Completed 500 Internal Server Error in 30ms (ActiveRecord: 11.2ms)
2019-07-31T19:42:16.567845+00:00 app[web.1]: F, [2019-07-31T19:42:16.567772 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5]
2019-07-31T19:42:16.567954+00:00 app[web.1]: F, [2019-07-31T19:42:16.567879 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "users" does not exist
2019-07-31T19:42:16.567957+00:00 app[web.1]: LINE 8: WHERE a.attrelid = '"users"'::regclass
2019-07-31T19:42:16.567959+00:00 app[web.1]: ^
2019-07-31T19:42:16.567961+00:00 app[web.1]: : SELECT a.attname, format_type(a.atttypid, a.atttypmod),
2019-07-31T19:42:16.567962+00:00 app[web.1]: pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
2019-07-31T19:42:16.567964+00:00 app[web.1]: c.collname, col_description(a.attrelid, a.attnum) AS comment
2019-07-31T19:42:16.567965+00:00 app[web.1]: FROM pg_attribute a
2019-07-31T19:42:16.567966+00:00 app[web.1]: LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2019-07-31T19:42:16.567968+00:00 app[web.1]: LEFT JOIN pg_type t ON a.atttypid = t.oid
2019-07-31T19:42:16.567969+00:00 app[web.1]: LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
2019-07-31T19:42:16.567971+00:00 app[web.1]: WHERE a.attrelid = '"users"'::regclass
2019-07-31T19:42:16.567972+00:00 app[web.1]: AND a.attnum > 0 AND NOT a.attisdropped
2019-07-31T19:42:16.567973+00:00 app[web.1]: ORDER BY a.attnum
2019-07-31T19:42:16.567975+00:00 app[web.1]: ):
2019-07-31T19:42:16.568064+00:00 app[web.1]: F, [2019-07-31T19:42:16.568002 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5]
2019-07-31T19:42:16.568171+00:00 app[web.1]: F, [2019-07-31T19:42:16.568104 #4] FATAL -- : [047c870d-8791-4ec7-90df-a7998b8b17c5] app/controllers/sessions_controller.rb:5:in `create'
I have a PostgreSQL add-on installed and it is running. (Otherwise the Rails server wouldn't even boot).
So why do I get this kind of errors on each request? I checked in console and if I do something like User.all I get this :
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "users" does not exist)
LINE 1: SELECT "users".* FROM "users" LIMIT $1
^
: SELECT "users".* FROM "users" LIMIT $1
This gets me so confused, what did I do wrong?
It sounds like you may have run rake db:migrate locally and not on Heroku. As a result, when you use the Heroku CLI, it doesn't recognize the users table because it doesn't yet exist.
I believe if you use have the pg gem in your Gemfile, Heroku's system will automatically setup a database for you. It's been a while since I've created a Heroku instance so don't quote me on that.
If you're unsure if you have PostgreSQL with your app, check the app's service add-ons area. Heroku also has more documentation on using PostgreSQL on their platform. It may be a good idea for you to reference their documentation for getting started with Rails as well.
Once you have your PostgreSQL database setup to work with your app, you use the Heroku CLI to migrate the database and create your users table:
heroku rake db:migrate

Heroku status 500 error after deploying Rails app

$heroku logs --tail
Started GET "/" for 46.188.182.234 at 2016-11-14 20:51:53 +0000
2016-11-14T20:51:53.840163+00:00 app[web.1]: [21c27115-512c-4e9e-bb24-8e1d8b0a46ca] Processing by StaticPagesController#home as HTML
2016-11-14T20:51:53.850654+00:00 app[web.1]: [21c27115-512c-4e9e-bb24-8e1d8b0a46ca] Rendering static_pages/home.html.erb within layouts/application
2016-11-14T20:51:53.856619+00:00 app[web.1]: [21c27115-512c-4e9e-bb24-8e1d8b0a46ca] Rendered static_pages/home.html.erb within layouts/application (5.7ms)
2016-11-14T20:51:53.859704+00:00 app[web.1]: [21c27115-512c-4e9e-bb24-8e1d8b0a46ca] Completed 200 OK in 19ms (Views: 11.1ms | ActiveRecord: 0.0ms)
2016-11-14T20:51:59.479887+00:00 heroku[router]: at=info method=GET path="/signup" host=damp-reef-30485.herokuapp.com request_id=79382bce-34fd-4faa-958c-15b6ce8adf1f fwd="46.188.182.234" dyno=web.1 connect=1ms service=64ms status=500 bytes=1733
2016-11-14T20:51:59.487241+00:00 app[web.1]: [79382bce-34fd-4faa-958c-15b6ce8adf1f] Started GET "/signup" for 46.188.182.234 at 2016-11-14 20:51:59 +0000
2016-11-14T20:51:59.495254+00:00 app[web.1]: [79382bce-34fd-4faa-958c-15b6ce8adf1f] Processing by UsersController#new as HTML
2016-11-14T20:51:59.509968+00:00 app[web.1]: [79382bce-34fd-4faa-958c-15b6ce8adf1f] Completed 500 Internal Server Error in 14ms (ActiveRecord: 1.6ms)
2016-11-14T20:51:59.511411+00:00 app[web.1]: [79382bce-34fd-4faa-958c-15b6ce8adf1f]
2016-11-14T20:51:59.511456+00:00 app[web.1]: [79382bce-34fd-4faa-958c-15b6ce8adf1f] ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "users" does not exist
2016-11-14T20:51:59.511457+00:00 app[web.1]: LINE 8: WHERE a.attrelid = '"users"'::regclass
2016-11-14T20:51:59.511457+00:00 app[web.1]: ^
2016-11-14T20:51:59.511458+00:00 app[web.1]: : SELECT a.attname, format_type(a.atttypid, a.atttypmod),
2016-11-14T20:51:59.511458+00:00 app[web.1]: pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
2016-11-14T20:51:59.511459+00:00 app[web.1]: (SELECT c.collname FROM pg_collation c, pg_type t
2016-11-14T20:51:59.511459+00:00 app[web.1]: WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation),
2016-11-14T20:51:59.511460+00:00 app[web.1]: col_description(a.attrelid, a.attnum) AS comment
2016-11-14T20:51:59.511460+00:00 app[web.1]: FROM pg_attribute a LEFT JOIN pg_attrdef d
2016-11-14T20:51:59.511461+00:00 app[web.1]: ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2016-11-14T20:51:59.511461+00:00 app[web.1]: WHERE a.attrelid = '"users"'::regclass
2016-11-14T20:51:59.511462+00:00 app[web.1]: AND a.attnum > 0 AND NOT a.attisdropped
2016-11-14T20:51:59.511462+00:00 app[web.1]: ORDER BY a.attnum
2016-11-14T20:51:59.511463+00:00 app[web.1]: ):
2016-11-14T20:51:59.511491+00:00 app[web.1]: [79382bce-34fd-4faa-958c-15b6ce8adf1f]
2016-11-14T20:51:59.511536+00:00 app[web.1]: [79382bce-34fd-4faa-958c-15b6ce8adf1f] app/controllers/users_controller.rb:17:in `new'
As you can see I get the home page but when I try to get signup form I got error. Everything is working ok locally, REST actions on users resource are working fine locally.
This output is interesting for me:
"PG::UndefinedTable: ERROR: relation "users" does not exist"
Please help, thanks.
You need to run migrations on Heroku, just run in the shell inside your project:
heroku run rails db:migrate
**This Issue was appeared because of some migration issue.To up migrations in heroku enter below command **
heroku run rake db:migrate
After this,check whether all migrations are up or not.If you get any issue check with that migration.

Need help in understanding Heroku logs

I have an rails 5 app which works perfectly in my local machine with PostgreSQL locally installed.
I pushed my app to heroku and the login page of the app appears correctly. But when I try to login to the app I get an error. Checking the logs give following output.
2016-10-26T04:21:48.515201+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=teamwallet.herokuapp.com request_id=94268ad1-1f86-4caf-80ae-61799fdddc5b fwd="182.57.131.204" dyno=web.1 connect=0ms service=1ms status=304 bytes=48
2016-10-26T04:21:54.410775+00:00 heroku[router]: at=info method=POST path="/" host=teamwallet.herokuapp.com request_id=52da63d5-afbd-4e8d-9d8a-51b04d077a21 fwd="182.57.131.204" dyno=web.1 connect=0ms service=7ms status=500 bytes=1669
2016-10-26T04:21:54.435325+00:00 app[web.1]: I, [2016-10-26T04:21:54.435257 #3] INFO -- : [52da63d5-afbd-4e8d-9d8a-51b04d077a21] Started POST "/" for 182.57.131.204 at 2016-10-26 04:21:54 +0000
2016-10-26T04:21:54.435994+00:00 app[web.1]: I, [2016-10-26T04:21:54.435933 #3] INFO -- : [52da63d5-afbd-4e8d-9d8a-51b04d077a21] Processing by SessionsController#login_attempt as HTML
2016-10-26T04:21:54.436093+00:00 app[web.1]: I, [2016-10-26T04:21:54.436045 #3] INFO -- : [52da63d5-afbd-4e8d-9d8a-51b04d077a21] Parameters: {"utf8"=>"✓", "authenticity_token"=>"xEvOCcjU8FZyt2OlypuNIifhucXsveX6TOGIXwkpddO7Dt9cEQyJrS/5Kw+trW35eo/jtv+A2IkuIzYHJUt4tQ==", "username_or_email"=>"admin", "login_password"=>"[FILTERED]", "commit"=>"Log In"}
2016-10-26T04:21:54.438633+00:00 app[web.1]: I, [2016-10-26T04:21:54.438587 #3] INFO -- : [52da63d5-afbd-4e8d-9d8a-51b04d077a21] Completed 500 Internal Server Error in 2ms (ActiveRecord: 1.7ms)
2016-10-26T04:21:54.439261+00:00 app[web.1]: F, [2016-10-26T04:21:54.439180 #3] FATAL -- : [52da63d5-afbd-4e8d-9d8a-51b04d077a21]
2016-10-26T04:21:54.439305+00:00 app[web.1]: F, [2016-10-26T04:21:54.439242 #3] FATAL -- : [52da63d5-afbd-4e8d-9d8a-51b04d077a21] ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "users" does not exist
2016-10-26T04:21:54.439306+00:00 app[web.1]: LINE 8: WHERE a.attrelid = '"users"'::regclass
2016-10-26T04:21:54.439307+00:00 app[web.1]: ^
2016-10-26T04:21:54.439308+00:00 app[web.1]: : SELECT a.attname, format_type(a.atttypid, a.atttypmod),
2016-10-26T04:21:54.439309+00:00 app[web.1]: pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
2016-10-26T04:21:54.439310+00:00 app[web.1]: (SELECT c.collname FROM pg_collation c, pg_type t
2016-10-26T04:21:54.439311+00:00 app[web.1]: WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation),
2016-10-26T04:21:54.439311+00:00 app[web.1]: col_description(a.attrelid, a.attnum) AS comment
2016-10-26T04:21:54.439312+00:00 app[web.1]: FROM pg_attribute a LEFT JOIN pg_attrdef d
2016-10-26T04:21:54.439313+00:00 app[web.1]: ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2016-10-26T04:21:54.439313+00:00 app[web.1]: WHERE a.attrelid = '"users"'::regclass
2016-10-26T04:21:54.439314+00:00 app[web.1]: AND a.attnum > 0 AND NOT a.attisdropped
2016-10-26T04:21:54.439315+00:00 app[web.1]: ORDER BY a.attnum
2016-10-26T04:21:54.439316+00:00 app[web.1]: ):
2016-10-26T04:21:54.439363+00:00 app[web.1]: F, [2016-10-26T04:21:54.439302 #3] FATAL -- : [52da63d5-afbd-4e8d-9d8a-51b04d077a21]
2016-10-26T04:21:54.439411+00:00 app[web.1]: F, [2016-10-26T04:21:54.439359 #3] FATAL -- : [52da63d5-afbd-4e8d-9d8a-51b04d077a21] app/models/user.rb:23:in `authenticate'
2016-10-26T04:21:54.439458+00:00 app[web.1]: F, [2016-10-26T04:21:54.439406 #3] FATAL -- : [52da63d5-afbd-4e8d-9d8a-51b04d077a21] app/controllers/sessions_controller.rb:11:in `login_attempt'
Your error says this:
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "users" does not exist
it's really easy to forget, but Heroku doesn't automatically run migrations.
From the heroku docs https://devcenter.heroku.com/articles/rake
# shell
heroku run rake db:migrate
heroku restart
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "users" does not exist
You forgot to run the migrations on heroku. Run this command on your console
heroku run rake db:migrate

Too many database connections created in rails 2.3.18 application

We have many models like the below one.
class User < ActiveRecord::Base
create_connection
def create_connection
# we have used this ActiveRecord::Base.configurations[current_environment] for connection_info
self.establish_connection connection_info
end
end
After using many models of similar types, many database connections are being created for the remote database. So we are getting the below error message for remote Postgres database.
2013-12-04T12:18:28.360740+00:00 app[web.1]: FATAL: too many connections for role "ycjcwpowlrmvhi"
Actually we have two applications on heroku and each application accesses the other's database.
How we can close these many database connection after executing the query?
We are also getting the below error some times.
2013-12-06T09:36:27.952787+00:00 app[web.1]: Processing StaffController#index (for 202.45.6.95 at 2013-12-06 09:36:27) [GET]
2013-12-06T09:36:16.453533+00:00 heroku[router]: at=info method=GET path=/javascripts/application.js?1386322453 host=demomanager.herokuapp.com fwd="202.45.6.95" dyno=web.1 connect=1ms service=3ms status=200 bytes=5025
2013-12-06T09:36:32.935950+00:00 app[web.1]:
2013-12-06T09:36:32.935950+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::UnableToSend: SSL error: decryption failed or bad record mac
2013-12-06T09:36:32.935950+00:00 app[web.1]: : SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
2013-12-06T09:36:32.935950+00:00 app[web.1]: FROM pg_attribute a LEFT JOIN pg_attrdef d
2013-12-06T09:36:32.935950+00:00 app[web.1]: ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2013-12-06T09:36:32.935950+00:00 app[web.1]: WHERE a.attrelid = '"tags"'::regclass
2013-12-06T09:36:32.935950+00:00 app[web.1]: AND a.attnum > 0 AND NOT a.attisdropped
2013-12-06T09:36:32.935950+00:00 app[web.1]: ORDER BY a.attnum
2013-12-06T09:36:32.935950+00:00 app[web.1]: ):
You should be able to change the number of database connections by changing the pool parameter in your database.yml and lowering that value.

Processing by UsersController#index as blank

I have a mobile version of my site that uses search. When I search just the layout loads but not the rest of the page. In the console it looks like it doesn't know what format to use. Any ideas on what is going wrong?
Here is the line:
Processing by UsersController#index as
and the full request:
Started GET "/users?mobile=1&search=a" for 127.0.0.1 at 2011-08-15 08:56:26 -040
0
Processing by UsersController#index as
Parameters: {"mobile"=>"1", "search"=>"a"}
←[1m←[35mUser Load (1.0ms)←[0m SELECT users.* FROM "users" WHERE (name ILIKE
'%a%') ORDER BY name
←[1m←[36mSQL (1.0ms)←[0m ←[1m SELECT a.attname, format_type(a.atttypid, a.att
typmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"users"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
←[0m
Rendered users/index.mobile.erb within layouts/application (59.0ms)
Completed 200 OK in 623ms (Views: 485.0ms | ActiveRecord: 2.0ms)

Resources