I'm trying to use a yaml config file as a very simple flat file db for a small rails app. The app has a single user, so I just need a way to store a username and password. The only thing is that I'd like to be able to edit the username and password while still inside the app and not require an app restart (so I can't load the YAML file inside an initializer...).
Any ideas on how I could accomplish this? I'm not married to the idea of using YAML, so if you have a better suggestion I'm all ears!
You really are better off using a database for this sort of thing, because it's how Rails is designed to work. The Rails default database is SQLite 3, which is a high-performance, reliable single file database.
Don't fight the defaults—use the right tool for the job.
You might wanna try iye for yaml editing on the fly. No DB needed, saves directly to file! Potentially you would just need something that tracks for file changes in your rails app and reloads your yaml file.
Here is the project page for iye: https://github.com/firmafon/iye
http://rubyforge.org/projects/rbyaml
http://yaml4r.sourceforge.net/doc/
(http://www.yaml.org/)
Related
I'm working on a Rails application and I am attempting to organize my secret_key_base and related important secrets (Think API keys, database credentials, etc.)
I am trying to find a way to setup something like the following, under /config
/config
credentials.yml.enc
master.key
/config/credentials
development.yml.enc
development.key
production.yml.enc
production.key
test.yml.enc
test.key
First Question:
Is it that secret_key_base exists in /config/credentials.yml.enc, which is loaded (first?) and then the credentials are loaded for the environment rails is running in? Or should I create a different secret_key_base for each environment?
Second Question:
No matter what I do, when I run in development or test, tmp/development_secret loads first. In addition, whenever I try to access my secrets in development, (Rails.application.secret_key_base) as referenced here: What's the correct way of defining secret_key_base on Rails 6, I run into an issue where I only ever receive nil when looking for secrets I've defined in my development.yml.enc, which I assume is because it's not loading anything in that file, it's going to tmp/development_secret and not finding anything else (Maybe I'm wrong.)
Goals:
Stop tmp/development_secret from being created, and instead access
secrets using the specific .yml.enc file depending on the
environment.
Understand why /config/credentials.yml.enc exists if it doesn't
load in all the environments. If it doesn't, then it isn't clear when it loads.
Why?
Using config/database.yml as an example, I want to store different creds for each environment, but none of them in version control. (I want nobody but a few to have production.) However, I want to access them the exact same way in all of my environments. Not having creds load in production because of an issue with a .yml file will crash my app, so I don't want to load them differently in test/development.
Put together a blog post about this because searching for documentation on this feature is painful. It's a really easy thing because then only the master.key, and production.key would need loaded as ENV variables which is great. (Or possibility just one of them.)
This really should be a simple, out-of-the-box thing, but it's hard to find real documentation on best practices.
I've found the answer, at least the one I'm looking for. You can have your initializer load whatever file you want by overriding the defaults.
config.credentials.content_path = 'config/credentials/development.yml.enc'
config.credentials.key_path = 'config/credentials/development.key'
https://edgeapi.rubyonrails.org/classes/Rails/Application.html
I'm currently doing my Final Year Project so I need to include code as part of the submission. Rails generate directories of different files, rather than a block of code that can be copy pasted. If there isn't is there a workaround to submit Rails code in a presentable manner.
Yes, it is technically possible. Here is an example of where it was done:
https://gist.github.com/clupprich/0e8b816883ca4dac6b7632a9e8351c48
This was done manually though, so if you have an existing application, the only way to do this is manually.
Also, I would not suggest this, as the directory structure is setup the way it is for a reason.
I need to edit yml files from Active Admin (database.yml, memcached.yml and others).
Are there any gem to solve this problem? If no, how can it be solved? Or maybe there are any way to keep config data in database instead of yml (anything but database.yml)?
Modifying settings in yaml files that are checked into the repository doesn't sound like a good thing to do. I suggest to store settings in a datastore like a database using http://github.com/ledermann/rails-settings
See also Ruby on Rails - Storing application configuration
I'm working on an Rails app which reads a CSV file on the server. However, the CSV file keeps getting updated.
My SQLite database is populated from the contents of this CSV file. Is there a way to run a Ruby script as soon as the file is modified and make changes in the database?
Sounds like you want the guard gem.
It integrates with stuff like libnotify, fsevent stuff so as to avoid polling, although it does also support polling as a less efficient fallback.
You might want to look into some parts of the Guard gem. Specifically the lib/vendor directory seem to contain the code you need for different platforms.
How is the CSV file updated? If it is updated by some program that you have control over, then you can add a code within the update routine to run the ruby script that upgrades the SQLite database as soon as the CSV file is updated.
Otherwise, you can do polling to check for a change in the CSV file.
I'm developing a Rails application and within that application I developed a Rake task that will read entries from a file and store them into the DB. Producing the code was no problem, but I'd like to know, where do I place the file that is read? Is there a convention for that, if yes, what is it?
I know I could have used the seed.rb file but is it ok, by the standards, to load and read a file from there?
Thanks in advance!
Yes, put the data you wish to load in the db/seeds.rb file and to load it run rake db:seed. This is what this file was designed to do.
I don't think there's a hard and fast Rails convention for this case. When it comes to seed data I put mine in a subfolder of db.
The yaml_db plugin dumps/loads content from/to the database from the file
rails_root/db/data.yml i'm not sure if this is by convention, however, the content is db related making the db folder an appropriate choice
Where to put stuff in Rails is a problem that I've been working around for a while. The question is whether your file can fit into one of the existing concerns. For instance, is it configuration information?
Anyway, perhaps a similar fit would be the schema.rb, which is put in the db directory. Do not modify the schema.rb with your data, of course: I'm merely suggesting that the db directory, or a subdirectory, might be a place to put your file(s).
On the other hand, if you don't see any directories that hold anything similar -- it's not one of the main categories in app, nor any of the main categories above that -- then you can just make up a name and use that.