Ruby On Rails and Heroku issue when writing in yaml files - ruby-on-rails

i'm working on rails and deploying my app on heroku.
I have build a translation system and in this system i need to write in yaml files
here is how i proceed :
File.open(ENV['PATH_TO_FILE'], 'w') do |f|
f.write hash.to_yaml
end
In localhost, it works perfectly, in the rails console, when i do
YAML::load_file(ENV['PATH_TO_FILE'])
i can see the new keys that have been added.
However, after deploying on Heroku, and running the same code in the rails console of heroku, no keys are added.
Does anyone have a solution to this problem?
Thanks in advance,

Heroku is a read-only file system, so writing to the yaml file won't be possible.
I would explore using a key/value store like redis or similar in place of the yaml file.

If you're on Bamboo stack, Heroku's filesystem is read only.
If you're on Cedar, each dyno has its own ephemeral filesystem. Check out this section of the Heroku docs
It's also possible the environment variable PATH_TO_FILE is not set correctly on heroku.
I recommend using the Figaro gem to manage your environment variables
https://github.com/laserlemon/figaro

Related

How to deploy file with sensitive data to heroku? (Rails project with Settingslogic gem)

Im using Settingslogic gem as alternative to Environment Variables, and I found it more convenient. But, how to deploy the application to Heroku if my file with configs is out of the repo? I mean, all configs I save in application.yml, which is included in .gitignore file (because there is sensitive data). And when I push it to heroku, server can't find this file and can't complete deploy.
I've tried to create the file from heroku bash, but after git push heroku master command, created file disappears, and deploy failes with the same error.
How can I implement this deploy with one config file, and how can I force heroku to read this file or store it if I have no it in Git? Thanks a lot!

Add Seeds file after Dokku build

I am using dokku-alot to deploy my Rails 4 app to my staging server and everything is working just swell.
One requirement I have with my current project is in regards to seed file data. I've had to keep my seeds.rb file out of version control because of sensitive information. However, I can't figure out how to add the seeds.rb file into the container after a build.
I've tried ssh root#myhost ap_name which gets me into the VM but even if I scp the files into there, the container doesn't see them. How can I drop a few files where my rails code is in the docker image?
Depending on how much information is in your seeds.rb file, you could use environmental variables. This the solution I ended up using.
You basically set the variable: config:set my-app SECRET=whateversupersecretinfo. Then in your code, you can extract that app variable by using ENV['SECRET']. (This works pretty much the same in Heroku) Not sure if that would solve your use case, but leaving this answer here for posterity.
subnote: In Node.js you can extract these variables like process.env.SECRET

How to access environment variables in coffeescript?

I have the following line in a js.coffee.erb file in a Rails 3.2 app.
<%= ENV['MY_KEY'] %>
The app is deployed to Heroku and when I run heroku config I see MY_KEY listed with a value.
Yet when I inspect the compiled javascript file there is a gap where ENV['MY_KEY'] value should be.
As far as I know it should be possible to access environment variables in a coffeescript asset. What could be going wrong here?
Your app's config vars are not available in the environment by default. To allow this, you can enable the Heroku Labs feature user-env-compile.

Rails untracked files to Heroku server

I am trying to deploy a rails app to heroku, and I know that their file system is read only, and it only contains the latest code. I am using a git repository, via this guide. I have a config file, holding passwords and other stuff that I don't want to track on git, but I have to upload them to heroku. How can I accomplish that?
Rather than store the confidential info in the app files, set them up as environment variables on heroku using config vars
Ref: https://stackoverflow.com/a/7832040/429758

reading a file in rails app deployed on heroku

I have a rails app deployed in heroku. In the app I have a simple txt file. I have a ruby script that loops on all records in this file, makes a URL out of it and then does other stuff with it.
I can not do this locally and then transfer the data to my heroku app.
From heroku console is it possible to run a traditional simple ruby script that would loop over a simple txt file in my heroku app?
yes you can read a text file, as long as it is pushed to Heroku with your code, the syntax is the same as reading files on your localhost
s = File.read("#{Rails.root}/app/assets/some_text_file.txt")
Heroku does not allow writing files

Resources