简体   繁体   中英

HEROKU AND Ruby on rails: Problems database

I'm new to ruby development and I had a problem with the database when deploying the project on heroku

I used postgresql as a database. Test created an entry in the database using the name:string field. Then I made a migration and decided to check the performance locally.

<h1><%= @name.name %></h1>

Locally the database works correctly. But when deploying this project on heroku, it gives the following picture.

enter image description here

Logs:

enter image description here

Database on heroku after migration enter image description here

From your log screenshot, I could see that the error is ActiveRecord::RecordNotFound (Couldn't find User with id=2) .

I am sure you're loading with something like User.find(2) in WelcomeController#show .

Just make sure User#2 exists or loading other existing User. If it doesn't solve your issue, please let me know. Good luck with your journey in learning Rails.

Like said above, the problem is that your record does not exist in production. If you added it locally, it's normal that is has not been added when pushed to Heroku, as the Database is reset.

There are many ways to fix your problem, here are two:

You can use the seed file in the folder db/seeds , in which you added your User.create!(id: 2) and run:

heroku run rails db:seed -a name_of_your_app

Or you can run a rails console from which you User.create!(id: 2) from Heroku to manually create that missing user:

heroku run rails c -a name_of_your_app

You should also learn how to read errors that get printed in the console, Record Not Found could have happened locally and has not much to do with Heroku. You should be able to tell what the problem is rather quickly if you learn to do so.

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