gem 'dotenv-rails' is required in gemfile test and development environments.
.env file is saved in root
I believe variables use correct syntax; USERNAME=username
I am using Rails 5.0.4
I have not required 'dotenv-rails' anywhere, as the docs do not suggest that I need to.
When playing in the console, the only way I can access the variables is by calling, Dotenv.load in each session. Suggesting that Dotenv.load should be called somewhere in config of my app.
Add Dotenv::Railtie.load to your config/application.rb
You may also need to restart Spring.
bin/spring stop
It will restart once you run another rails command.
In case this helps anyone else:
This happened to me once. As it turns out, I was mistakenly passing RAILS_ENV=production in the environment variables, disabling it.
For me the bin/spring stop fixed the issue and I am using ruby 3.1.2 and rails 7.0.4.
Related
I'm trying to set an auth token for my gemfile to access a private git repo.
i.e.
gem 'mygem', git: "https://ENV['GITHUB_AUTH_TOKEN']:x-oauth-basic#github.com/my_account/my_repo.git", tag: "0.0.1"
I can't work out how to store this in Figaro but make it accessible to bundle when I run bundle install.
Very similar to
This question
Except that rather than having a config/heroku_env.rb I have an config/application.yml file.
I'm sure the answer is ridiculously straightforward.
I'd like to keep it in that file as it keeps everything neatly in one place, but if not I can put it somewhere specific so long as it lines up with heroku nicely.
Any ideas?
I have found one way to do it that works, it's slightly annoying in that you have to keep the credentials in two different places.
.bundle
BUNDLE_GITHUB__COM: <auth_token>:x-oauth-basic
Gemfile
gem 'mygem', git: "https://github.com/my_account/my_repo.git", tag: "0.0.1"
# Note that you don't put anything in here, bundler sorts it out automagically
And then
heroku config:set BUNDLE_GITHUB__COM=<auth_token>:x-oauth-basic
Works.
Annoying because now application.yml has different content to my heroku file. But so be it.
Update:
Better solution
Just put
BUNDLE_GITHUB__COM: <auth_token>:x-oauth-basic
into both your application.yml and heroku config.
I wish someone had documented that somewhere, would have saved me a ton of trouble...
I think you need to run
figaro heroku:set -e production
for it to set set the environment variables in your config/application.yml file as Heroku environment variables.
I am using gem 'dotenv-rails', '~> 0.9.0' to load environment variables into a Rails 4.0.5 app. I have a .env file along with a .env.test. All is working well in development, however when it comes to testing, which I do with rspec, it is failing to set the environment variables.
I have gone into Rails console in testing environment and I can see they are set to nil.
Any tips on how to get dotenv to load in testing?
This is a late answer, but for anyone who may find it helpful, add this to spec_helper.rb
require 'dotenv'
Dotenv.load('.env')
Also, Dotenv.load can accept a list, for example:
Dotenv.load(
'.env.local',
'.env.test',
'.env'
)
If you have test specific environemnt config, You can use Dotenv.overload to overwrite other env config. Just add the following to your spec_helper.rb or rails_helper.rb.
require "dotenv"
Dotenv.overload ".env.test"
Dotenv.overload ".env.test.local"
Dotenv.load(File.expand_path("../../.env.#{Rails.env}", __FILE__))
upon researching Paritosh's answer I found that the file needed to specified per environment.
put this line in Dotenv::Railtie.load in rspec setting or any page in which you want to load dot env file.
I would like to use gems 'better_errors' and 'binding_of_caller' for my debugging in rails app, but i DON'T want to include those in Gemfile. Is it possible to do? My first thought was to simply
gem install better_errors
gem install binding_of_caller
but it doesnt work, i mean installation finishes without problems, but thats it, gem doesnt seem to work at all when i run my app on localhost. Do I need some kind of config set, anybody?
but i DON'T want to include those in Gemfile. Is it possible to do?
Yes, it is possible. You can just download the respective directories in desire folder (ex. lib) and add that gem class in your initializer so it will be loaded at the time of starting. Configuration varies as per gem.
My first thought was to simply .... but it doesnt work,
Ofcourse, it wont. How can your rails app magically detects without knowing it that you have better way to show error. It is simply saying like you have cancer formula and doctors automatically applied that formula to there patient without you telling them. There should be some commucaition between two parties rails-app and gem so they can coordinate and work better.
Do I need some kind of config set, anybody?
Yes, explained above.
i dont want to force those gems on my coworkers. KRUKUSA any more details? // said in comment
Yes, including this gems in your rails app can do this job. This extension will be available automatically to your worked. (no force applied :P)
it looks like all you want to not show those gems to other co-worker, if so, you can use this trick with git.
To achieve this thing, first simply add the gems in your gemfile, run bundle and then make it untrackable with git. You can put Gemfile and Gemfile.lock in your .gitignore file. or you can add the first add the gems and mark it ignore with below command. Read more here
git update-index --assume-unchanged Gemfile Gemfile.lock
Another possibility would be to create your own environment and use it accordingly.
Have your own configuration for myenv:
$ cp config/environments/{development,myenv}.rb
In config/database.yml, add the environment myenv and use the same config as development:
development: &development
<rest of the code you have on config/databases.yml>
...
myenv:
<< *development
In Gemfile add your custom gems to use on your mydev group:
group :myenv do
gem 'better_errors'
gem 'binder_of_caller'
end
Run rails and rake with RAILS_ENV like this: RAILS_ENV=myenv rails c
The advantage of this approach is that you still get the updates from Gemfile from the repo, and if you need to add a gem in the Gemfile for everybody to see, you still can.
Also, nobody will see the gems you installed inside the myenv group in your Gemfile.
I've added awesome_print to my ~/.irbrc file like so:
require 'ap'
Inside a Rails project directory, if I run irb it loads the gem fine, because I've already installed the gem locally. But if I run rails console, it spits out this error:
cannot load such file -- ap
How can I resolve this? I am guessing that it's looking for the gem in the app's Gemfile, but I don't want to add it to the Gemfile because I don't want other developers requiring that dependency. I only want to use awesome_print on my machine.
I am also using rbenv, if that is of any help.
There is this trick.
What you need to do is
# Copy the definition of the debundle! method into your ~/.irbrc
# Call 'debundle!' from IRB when you need to.
(as explained at the top of the file)
The text as it appears on the referred to site:
debundle.rb allows you to require gems that are not in your Gemfile when inspecting
programs that are run with Bundler.
Use at your own risk!
Paste the code of debundle.rb and you are done! A good place would be your .irbrc file
before requiring irbtools.
The code is directly taken from pry-debundle.
Please look there for any further information. This repo exists to simplify debundling
without using the pry repl.
I'm debugging the nested_set gem which is under C:\Ruby187\lib\ruby\gems\1.8\gems\nested_set-1.6.4\lib\nested_set
I make changes to files in there, but it seems to have no effect when I refresh the page. I deleted the cache file in C:\Ruby187\lib\ruby\gems\1.8\cache to no effect.
Is there some sort of caching I should know about?
If it is rails you need to restart your server
Have you run gem env from the command line to check that's what's being used?