DEV Community

Cisco Vlahakis
Cisco Vlahakis

Posted on

Using Firestore with ERB view and table

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.

  1. 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
Enter fullscreen mode Exit fullscreen mode

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.

  1. 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>
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)