Instead of storing static data a database, you can create a structured YML or JSON file and parse it from your Ruby/Rails app.
I really like the data structure of a .yml
file, because it is much cleaner to write than .json
.
Here’s an example list of schools in the YAML format:
# db/view_data/schools.yml
- name: Austin High School
team_name: Austin Mustangs
city: Houston
state: TX
primary_color: green
secondary_color: white
- name: Bellaire High School
team_name: Bellaire Cardinals
city: Houston
state: TX
primary_color: red-lighter
secondary_color: white
- name: Carnegie Vanguard High School
team_name: Carnegie Vanguard Rhinos
city: Houston
state: TX
primary_color: blue
secondary_color: red-lighter
You can parse this data (convert it into a Hash or Array) using Ruby on Rails native yaml parsers!
- Parse a local YAML file with pure Ruby :
require 'yaml'
path = "/Users/yaroslavshmarov/Documents/GitHub.nosync/schools.yml"
@schools = YAML::load File.open(path)
@schools.first.fetch('name')
# => "Austin High School"
Source: Ruby YAML docs
- Parse a YAML file inside a Rails app:
# a controller action
# @schools = YAML::load File.open("#{Rails.root.to_s}/db/fixtures/schools.yml") # ruby way
@schools = YAML.load_file('db/fixtures/schools.yml') # rails way
@schools.inspect
Render the results in a view:
# a view
<% @schools.each do |school| %>
<%= school.fetch('name') %>
<%= school['name'] %>
<% end %>
Source: Rails YAML.load_file docs
That’s it! 🤠
Top comments (0)