You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/essential/02_active_record.md
+43-54Lines changed: 43 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ In this guide, we will
10
10
- deploy the changes to the publicly running Heroku application
11
11
12
12
## 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).
14
14
15
15
## Adding the ActiveRecord model
16
16
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
19
19
rails generate model Person first_name:string last_name:string role:integer
20
20
```
21
21
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
23
23
24
24
```sh
25
-
rake db:migrate
25
+
rails db:migrate
26
26
```
27
27
28
28
which should update the database schema in `db/schema.rb`.
29
29
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:
32
32
33
33
```ruby
34
34
classPerson < ApplicationRecord
35
35
enum role: [:client, :partner, :staff]
36
36
end
37
37
```
38
38
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`:
40
40
41
41
```ruby
42
42
seeded_persons = [
@@ -61,35 +61,49 @@ end
61
61
and run
62
62
63
63
```sh
64
-
rake db:seed
64
+
rails db:seed
65
65
```
66
66
67
-
to add those first persons to the database!
67
+
to add those persons to the database!
68
68
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`.
70
70
71
71
```ruby
72
-
classDemo::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
+
classPersonsController < ApplicationController
84
+
85
+
defindex
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.
@@ -98,9 +112,11 @@ class Demo::App < Matestack::Ui::App
98
112
end
99
113
```
100
114
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!
102
118
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!
104
120
105
121
## Saving the status quo
106
122
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
109
125
git add .&& git commit -m "Introduce person model including seeds, add it to matestack/demo/app.rb"
110
126
```
111
127
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
-
139
128
## 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.
141
130
142
131
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