Skip to content

Commit c22e152

Browse files
author
Nils Henning
committed
[TASK][REFACTOR] rework 02_active_record guide
1 parent 98839cd commit c22e152

File tree

1 file changed

+43
-54
lines changed

1 file changed

+43
-54
lines changed

docs/guides/essential/02_active_record.md

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ In this guide, we will
1010
- deploy the changes to the publicly running Heroku application
1111

1212
## Prerequisites
13-
We expect you to have successfully finished the [previous guide](guides/essential/01_setup.md) and no uncommited changes in your project.
13+
We expect you to have successfully finished the [previous guide](guides/essential/01_setup.md).
1414

1515
## Adding the ActiveRecord model
1616
First, we need to create an ActiveRecord model and add the corresponding table to the database. This is quickly achieved by running
@@ -19,24 +19,24 @@ First, we need to create an ActiveRecord model and add the corresponding table t
1919
rails generate model Person first_name:string last_name:string role:integer
2020
```
2121

22-
in the terminal. After the command has finished, you should see a new migration in `db/migrations/` and a newly added model in `app/models/person.rb`. To then apply those changes, you need to run
22+
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
2323

2424
```sh
25-
rake db:migrate
25+
rails db:migrate
2626
```
2727

2828
which should update the database schema in `db/schema.rb`.
2929

30-
## Updating the model, adding seeds and displaying app
31-
To make use of the `role`-enum we already prepared in the database, update `app/models/person.rb` to look like this:
30+
## Updating the model, adding seeds and displaying an index page
31+
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:
3232

3333
```ruby
3434
class Person < ApplicationRecord
3535
enum role: [:client, :partner, :staff]
3636
end
3737
```
3838

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

4141
```ruby
4242
seeded_persons = [
@@ -61,35 +61,49 @@ end
6161
and run
6262

6363
```sh
64-
rake db:seed
64+
rails db:seed
6565
```
6666

67-
to add those first persons to the database!
67+
to add those persons to the database!
6868

69-
To finish things up for now, let's display all the new persons within the `matestack` app! To achieve this, update the contents of `app/matestack/demo/app.rb` to look like this:
69+
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`.
7070

7171
```ruby
72-
class Demo::App < Matestack::Ui::App
72+
Rails.application.routes.draw do
73+
root to: 'demo#first_page'
74+
get '/second_page' to: 'demo#second_page'
75+
76+
resources :persons, only: [:index]
77+
end
78+
```
79+
80+
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.
81+
82+
```ruby
83+
class PersonsController < ApplicationController
84+
85+
def index
86+
render Demo::Pages::Persons::Index
87+
end
88+
89+
end
90+
```
91+
92+
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.
93+
94+
```ruby
95+
class Demo::Pages::Persons::Index
7396

7497
def prepare
7598
@persons = Person.all
7699
end
77100

78101
def response
79-
header do
80-
heading size: 1, text: 'Demo App'
81-
transition path: :first_page_path, text: 'First page'
82-
br
83-
transition path: :second_page_path, text: 'Second page'
84-
end
85-
main do
86-
page_content
87-
ul do
88-
@persons.each do |person|
89-
li do
90-
plain person.first_name
91-
strong text: person.last_name
92-
end
102+
ul do
103+
@persons.each do |person|
104+
li do
105+
plain person.first_name
106+
strong text: person.last_name
93107
end
94108
end
95109
end
@@ -98,9 +112,11 @@ class Demo::App < Matestack::Ui::App
98112
end
99113
```
100114

101-
What happens here? In the `prepare`-statement, you're fetching all the person instances from your database, and within the `main`-tag each person's name gets displayed (last names in **bold** for extra emphasis) inside an unordered list (`ul`). Run `rails s` and head over to [localhost:3000](http://localhost:3000/) to check the result!
115+
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.
116+
117+
Run `rails s` and head over to [localhost:3000/persons/index](http://localhost:3000/persons/index) to check the result!
102118

103-
Of course, this is a very basic approach that we will iterate upon in the following parts of this guide series!
119+
Of course, this is a very basic approach that we will iterate and improve in the following parts of this guide series!
104120

105121
## Saving the status quo
106122
As usual, we want to commit the progress to Git. In the repo root, run
@@ -109,34 +125,7 @@ As usual, we want to commit the progress to Git. In the repo root, run
109125
git add . && git commit -m "Introduce person model including seeds, add it to matestack/demo/app.rb"
110126
```
111127

112-
## Deployment
113-
After you've finished all your changes and commited them to Git, trigger another deployment via
114-
115-
```sh
116-
git push heroku master
117-
```
118-
119-
to publish your changes. If you visit your site now, you will find that it is broken! But no worries, this is easily fixed: This time, since we've changed the schema in the database, we also need to take an extra step and migrate the database by running
120-
121-
```sh
122-
heroku run rake db:migrate
123-
```
124-
125-
After the migration finished successfully, your site is available again, but there's no visible change - there are no persons in the database yet. To change this, trigger the seeds by running
126-
127-
```sh
128-
heroku run rake db:seed
129-
```
130-
131-
Finally, you should be able to check the changes via
132-
133-
```sh
134-
heroku open
135-
```
136-
137-
and see a list of all the persons in your database - nice!
138-
139128
## Recap & outlook
140-
We have updated the app to use a working database model, added some dummy records and displayed them in the `matestack` app.
129+
We have updated the app to use a working database model, added some records and displayed them on an index page.
141130

142131
Let's continue and build even cooler stuff by heading directly to the [next part of the series](/guides/essential/03_index_show.md).

0 commit comments

Comments
 (0)