[Ruby] Solution when API key is not found after deploying on Rails heroku

1 minute read

Implemented functions

Get latitude and longitude from the address entered using gem geocoder. Obtain a Google API Key and display the map on the application.

Problem encountered

It worked fine in the development environment, but in the production environment after deployment When checking the operation, the map was not displayed for the entered address and nothing was returned.

I guess

When I check the console, it says No Apikeys and Invalidkey, so I guess the API key is the cause.

Since I lacked knowledge of API, is there a problem with the code I am writing in the development environment or with the Google API settings? Thinking that, I will dig deeper, but I can not find any articles that can be solved.

By the way, I used gem "dotenv-rails" for the API key, put it in an environment variable in .env and hide it in .gitignore.

If you include Google Maps Platform API Checker which is an extension of Chrome, I was able to confirm that the error character and a sentence of the code containing the API key are displayed, but the API key itself is not found.

In other words, the environment variable is not called.

problem solving

I couldn’t help being caught in the swamp when I squirmed around here and there (laughs) I decided to calm down and think about it.

**…Is it because of deploying to heroku? ? **

So when I go Google, this is bingo!

Heroku doesn’t deploy files in .gitignore He said that he needed to set environment variables with heroku config:set.

[heroku] Environment variable operation

Set environment variables in heroku with reference to this article.

I was able to solve it safely! !! !!