Matestack Ui Core
AboutMatestack Ui CoreMatestack Ui VueJsMatestack Ui Bootstrap
1.5
1.5
  • Welcome
  • Getting started
    • Installation & Update
    • Concepts & Rails Integration
    • Quick Start
    • Support & Feedback [WIP]
    • Common Issues [WIP]
  • UI Components
    • Component Overview
    • Rails Integration
    • Component Registry
    • General Component API
    • Haml Components
    • Reusing Views or Partials
    • Testing [WIP]
  • Reactivity
    • Reactivity Overview
    • Rails Integration
    • Actions
    • Forms
    • Async
    • Cable
    • Isolated
    • Collection
    • Custom Vue.js Components
    • Vue.js Event Hub
    • Vuex [WIP]
  • SPA-like Apps
    • SPA Overview
    • Rails Integration
    • App API
    • Page API
    • Transitions
    • Authorization
    • Tutorial
      • Creating a SPA-like App with Matestack
      • Essential Guide 1: Setup
      • Essential Guide 2: ActiveRecord & Database
      • Essential Guide 3: Person Index, Show, Transition
      • Essential Guide 4: Forms & Actions (Create, Update, Delete)
      • Essential Guide 5: Toggle Component
      • Essential Guide 6: Async Component
      • Essential Guide 7: Partials and custom components
      • Essential Guide 8: Collection and async component
      • Essential Guide 9: Custom Vue.js components
      • Essential Guide 10: Styling and Notifications
      • Essential Guide 11: Authentication
      • Essential Guide 12: Heroku Deployment with Postgres
      • Essential Guide 13: Wrap Up & Outlook
  • Components API
    • Core Components
      • Abbr
      • Address
      • Area
      • Article
      • Aside
      • B
      • Bdi
      • Bdo
      • Blockquote
      • Button
      • Br
      • Caption
      • Cite
      • Code
      • Data
      • Datalist
      • Dd
      • Del
      • Details
      • Dfn
      • Dialog
      • Div
      • Dl
      • Dt
      • Em
      • Fieldset
      • Figure
      • Footer
      • Hr
      • Icon
      • Iframe
      • Img
      • Ins
      • Input
      • Header
      • Heading
      • Kbd
      • Label
      • Legend
      • Link
      • Lists
      • Main
      • Mark
      • Map
      • Meter
      • Nav
      • Noscript
      • Object
      • Option
      • Optgroup
      • Output
      • Paragraph
      • Param
      • Picture
      • Pg
      • Plain
      • Pre
      • Progress
      • S
      • Samp
      • Section
      • Select
      • Small
      • Span
      • Sup
      • Sub
      • Strong
      • Table
      • Template
      • Textarea
      • U
      • Unescaped
      • Q
      • Rails View
      • Rp
      • Rt
      • Ruby
      • Var
      • Video
      • Wbr
      • Youtube
    • Reactive Core Components
      • Action
      • Async
      • Cable
      • Collection
      • Form
      • Form Input
      • Form Checkbox
      • Form Radio
      • Form Select
      • Form Submit
      • Form Textarea
      • Onclick
      • Transition
      • Toggle
  • Integrations
    • Action Cable
    • Devise
    • CSS Frameworks [WIP]
    • Third Party JavaScript [WIP]
    • Third Party Ruby Gems [WIP]
  • Matestack Addons
    • Create your own Addon [WIP]
  • Community
    • Discord
    • Contribute
  • About
    • Core Team [WIP]
    • Sponsoring [WIP]
    • Legal Details [WIP]
Powered by GitBook
On this page
  • Introduction
  • Prerequisites
  • Adding the ActiveRecord model
  • Updating the model, adding seeds and displaying an index page
  • Saving the status quo
  • Recap & outlook

Was this helpful?

Edit on GitHub
  1. SPA-like Apps
  2. Tutorial

Essential Guide 2: ActiveRecord & Database

PreviousEssential Guide 1: SetupNextEssential Guide 3: Person Index, Show, Transition

Last updated 3 years ago

Was this helpful?

Demo: Github Repo:

Welcome to the second part of our tutorial about building a web application with matestack.

Introduction

In the , we created a new project, installed the necessary libraries, added a demo matestack app featuring two matestack pages, and deployed it using Heroku.

In this guide, we will

  • create an ActiveRecord model to work with throughout this series

  • add some seeded data and migrate the database

Prerequisites

We expect you to have successfully finished the .

Adding the ActiveRecord model

First, we need to create an ActiveRecord model and add the corresponding table to the database. This is quickly achieved by running

rails generate model Person first_name:string last_name:string role:integer

in the terminal. After the command has finished, you should see a new migration in db/migrations/ and a newly added model app/models/person.rb. To then apply those changes, you need to run

rails db:migrate

which should update the database schema in db/schema.rb.

Updating the model, adding seeds and displaying an index page

The role database field should represent different roles which we define as an enum in our person model. Update app/models/person.rb to look like this:

class Person < ApplicationRecord
    enum role: [:client, :partner, :staff]
end

Great! Now, let's populate the database with some "fake" persons. Therefore add the following content to db/seeds.rb:

seeded_persons = [
    {first_name: 'Harris', last_name: 'Bees', role: :client},
    {first_name: 'Abigail', last_name: 'Salte', role: :client},
    {first_name: 'Woodrow', last_name: 'Trembly', role: :client},
    {first_name: 'Murray', last_name: 'Fedorko', role: :client},
    {first_name: 'Michaele', last_name: 'Kritikos', role: :client},
    {first_name: 'Sammie', last_name: 'Scovill', role: :client},
    {first_name: 'Xavier', last_name: 'Accosta', role: :partner},
    {first_name: 'Otis', last_name: 'Morro', role: :partner},
    {first_name: 'Omer', last_name: 'Ottman', role: :partner},
    {first_name: 'Marlo', last_name: 'Yousko', role: :staff},
    {first_name: 'Manuel', last_name: 'Venn', role: :staff}
]

seeded_persons.each do |person|
    Person.create(person)
end

and run

rails db:seed

to add those persons to the database!

To finish things up for now, let's display all the new persons on an index page inside our app! To achieve this, we add an index route for our person model in routes.rb.

Rails.application.routes.draw do
  root to: 'demo#first_page'
  get '/second_page' to: 'demo#second_page'

  resources :persons, only: [:index]
end

After that we create the corresponding person controller in app/controllers/person_controller.rb which will handle the index action. The index action should render the persons index page which we will create afterwards. Do not forget to define the matestack_app which should be used as a layout.

class PersonsController < ApplicationController
  matestack_app Demo::App

  def index
    render Demo::Pages::Persons::Index
  end

end

Now we create our person index page. Because it should be rendered inside our demo app, it belongs to the demo namespace and as a page under the pages section. So we create our person index page under app/matestack/demo/pages/persons with the name index.rb. This index page should render a list of all persons.

class Demo::Pages::Persons::Index

  def prepare
    @persons = Person.all
  end

  def response
    ul do
      @persons.each do |person|
        li do
          plain person.first_name
          strong text: person.last_name
        end
      end
    end
  end

end

What happens in this page? Before calling the response method of a page, app or component the prepare method gets evaluated. In this case we fetch all the persons from the database and assign the result to the instance variable @persons in the prepare statement. Inside our response method we can access this instance variable and iterate over it to create li tags containing the plain person first name and the lastname inside a strong tag.

Of course, this is a very basic approach that we will iterate and improve in the following parts of this guide series!

Saving the status quo

As usual, we want to commit the progress to Git. In the repo root, run

git add . && git commit -m "Introduce person model including seeds, add it to matestack/demo/app.rb"

Recap & outlook

We have updated the app to use a working database model, added some records and displayed them on an index page.

Run rails s and head over to to check the result!

Let's continue and build even cooler stuff by heading directly to the .

Matestack Demo
Matestack Demo Application
previous guide
previous guide
localhost:3000/persons/index
next part of the series