Using Firestore with ERB view and table
Cisco Vlahakis
Posted on November 19, 2023
Here is a simple example for querying data from Firestore to be displayed in a table. Ensure that you have Firestore properly installed and configured.
- Define a route that gets the documents (in this example, meal_events) from Firestore and renders a view:
require 'sinatra'
require 'firebase'
get '/meal_events' do
base_uri = 'https://<your-firebase-app>.firebaseio.com/'
firebase = Firebase::Client.new(base_uri)
response = firebase.get('meal_events')
@meal_events = response.body
erb :meal_events
end
In this handler, we’re using the firebase gem to connect to Firestore, then we’re fetching the meal events and storing them in the instance variable @meal_events. This instance variable will be available in our view.
- Next, in your views/meal_events.erb file, you can iterate over @meal_events to generate your table rows:
<table>
<thead>...</thead>
<tbody>
<% @meal_events.each do |meal_event| %>
<tr>
<td><%= meal_event.fetch('name') %></td>
<td><%= meal_event.fetch('parent_meal_event') %></td>
<td><%= meal_event.fetch('min_foods') %></td>
<td><%= meal_event.fetch('max_foods') %></td>
</tr>
<% end %>
</tbody>
</table>
This will create a new table row for each meal event, with the table data filled in from the meal event.
Remember to replace <your-firebase-app>
with your actual Firebase app’s name!
💖 💪 🙅 🚩
Cisco Vlahakis
Posted on November 19, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
privacy Caught in the Crunch My Journey from Snacks to 2 Million Exposed Users Privacy
November 30, 2024