reading a file in rails app deployed on heroku - ruby-on-rails

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

Related

Ruby on Rails app on Heroku file upload not working

I'm new to Rails and Heroku as well.
Following tutorials I succeed to upload my app to Heroku.
My app is enable us to upload csv file to uploads folder in public.
So I can upload file in heroku deployed app and show table as I want.
But after 30 mins or so those files are removed automatically and showing error that no such file in public/uploads folder.
It is properly working on my local which is using sqlite3.
But heroku want to use psql for production mode so I changed production mode to use psql.
So if I upload csv file then it is stored in 'public/uploads' folder.
I think heroku app is automatically update to github repo even I didn't execute 'git push heroku master' command.
In my local repository files are stored but they are run on development mode.
How can I make upload functionality fully working?
Any help would be appreciate.
Ephemeral filesystem is just the way heroku works, as explained in the article - uploaded files will be removed when the instance is restarted.
What you should do is to upload those files for example to S3 (https://devcenter.heroku.com/articles/s3), you can achieve this with CarrierWave gem with Fog gem but there are other possibilities as explained in this thread here: https://www.reddit.com/r/rails/comments/2k9sq4/heroku_any_files_you_upload_will_not_be_saved/

Rails server not working when copying back from staging deploy

I ssh copied a rails app from a staging server because the development repository has been lost. My goal is to create a new development code base using the deployed code as a source. So far I have removed a hidden .bundle folder and replaces several aliases with folders and files. I then ran bundle install. For the database I did a sql dump from staging and used it to build a development database. I think I'm ready to run rails server. But when I try to run rails server in the base directory. It gives me the rails command line help as if I was running rails s in a directory with no app.
I'm not even sure if it is possible to reverse a deploy this way. I've looked at the rails guide on the app initialization process and all the files seem to be in place.
Remember that for Rails to start up, you need bin/rails, bin/bundle, config/boot, etc.
If you restore those files, it should work again.

Ruby On Rails and Heroku issue when writing in yaml files

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

Rails & Heroku: Running a script through rails runner using local files

So, I have an app deployed on Heroku, and I'm trying to populate the database on the app through a script I wrote. I have the database in a text file, and the script runs through the file and populates the database. I don't want to push the database file to the heroku server, since it's a very large file.
Is there any way to do this on Heroku? It works fine locally, but I can't get it to work on the Heroku server.
I've tried
heroku run rails runner PATH/TO/SCRIPT LOCAL/PATH/TO/DATABASE --app my_app
to no avail.
To run a local script on Heroku:
irbify.rb script.rb | heroku run rails console --app=my_app
irbify.rb is a silly tiny script I wrote to convert a script to a single eval statement.
Regarding passing data: you can serialize it in some form and put it inside script.
Hope it helps someone.
UPDATE: this does not work well anything beyond trivial datasets.
You can also upload your script to a gist and then do:
binding.eval(open("your gist raw url").read)
I had to use global variables ($dollar_prefixed) since the context would not get filled with the variables (using Pry), otherwise it went well.

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

Resources