Skip to content

Commit 462e62f

Browse files
author
Nils Henning
committed
[TASK][REFACTOR] rework 03_index_show_transitions guide
1 parent 0a0cd29 commit 462e62f

File tree

1 file changed

+47
-53
lines changed

1 file changed

+47
-53
lines changed

docs/guides/essential/03_index_show_transition.md

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@
22
Welcome to the third part of the 10-step-guide of setting up a working Rails CRUD app with `matestack-ui-core`!
33

44
## Introduction
5-
In the [previous guide](guides/essential/02_active_record.md), we added an ActiveRecord model to our project, added some fake persons to our database and displayed them on our `matestack` app.
5+
In the [previous guide](guides/essential/02_active_record.md), we added an ActiveRecord model to our project, added some fake persons to our database and displayed them on an index page.
66

77
In this guide, we will
8-
- add proper page to display all the persons in our database
98
- a detail page for every person
10-
- dive into the concept of page transitions by switching back and forth between the list of all persons and the detail page
9+
- dive more into the concept of page transitions
1110

1211
## Prerequisites
13-
We expect you to have successfully finished the [previous guide](guides/essential/02_active_record.md) and no uncommited changes in your project.
12+
We expect you to have successfully finished the [previous guide](guides/essential/02_active_record.md).
1413

15-
## Person controller & routes
16-
Let's kick it off by creating a dedicated controller for our **person** model. Add the content below to `app/controllers/persons_controller.rb`:
14+
## Update person controller and routes
15+
16+
We want to add a detail page for our persons. In order to do that we need to update our `routes.rb` to include the show action for persons.
17+
Also now is a good time to swap our rails root route to our persons index action.
18+
19+
```ruby
20+
Rails.application.routes.draw do
21+
root to: 'persons#index'
22+
23+
resources :persons, only: [:index, :show]
24+
end
25+
```
26+
27+
Next we need to add the show action to our person controller. Inside it we find the person matching the id from the path, like you would normally do in Rails.
1728

1829
```ruby
1930
class PersonsController < ApplicationController
@@ -31,84 +42,79 @@ class PersonsController < ApplicationController
3142
end
3243
```
3344

34-
Also, make sure to update the routes like this:
35-
36-
```ruby
37-
Rails.application.routes.draw do
38-
root to: 'persons#index'
39-
40-
resources :persons, only: [:index, :show]
41-
end
42-
```
43-
4445
## Page transitions
45-
In `app/matestack/demo/app.rb`, replace the contents with the code snippet below:
46+
In `app/matestack/demo/app.rb`, replace the contents with the code snippet below in order to add a navigation transition to the root path in our app layout.
4647

4748
```ruby
4849
class Demo::App < Matestack::Ui::App
4950

5051
def response
52+
nav do
53+
transition path: root_path, text: 'Persons'
54+
end
5155
header do
5256
heading size: 1, text: 'Demo App'
5357
end
5458
main do
55-
page_content
59+
yield_page
60+
end
61+
footer do
5662
hr
57-
transition path: :persons_path, text: 'All persons'
63+
small text: 'These guides are provided by matestack'
5864
end
5965
end
6066

6167
end
6268
```
6369

64-
We removed the prepare statement and will do the data fetching to the page level instead. Also, the transition links to the first two pages were removed and a transition to the `main` block of the `Demo::App` added. By doing this, we make sure the person index page will be reachable from all pages. Also, the horizontal ruler (`hr`) tag makes it easier to distinguish between the wrapping `matestack` app and the `page_content`!
65-
6670
## Person index page
67-
Create a new folder called `persons` in `app/matestack/demo/pages/`, and move the `first_page.rb` and `second_page.rb` there.
6871

69-
For the index page (where all the persons in the database get displayed), rename the `first_page.rb` to `index.rb` and add the content below:
72+
In order to view the details of a person we add a transition to every person on the index page linking to the persons show page.
7073

7174
```ruby
72-
class Demo::Pages::Persons::Index < Matestack::Ui::Page
75+
class Demo::Pages::Persons::Index
7376

74-
def prepare
75-
@persons = Person.all
76-
end
77+
def prepare
78+
@persons = Person.all
79+
end
7780

78-
def response
81+
def response
7982
ul do
8083
@persons.each do |person|
8184
li do
82-
plain "#{person.first_name} #{person.last_name} "
83-
transition path: :person_path, params: {id: person.id}, text: '(Details)'
85+
plain person.first_name
86+
strong text: person.last_name
87+
transition text: 'Details', path: person_path(person)
8488
end
8589
end
8690
end
87-
end
91+
end
8892

8993
end
9094
```
9195

92-
Like before, we loop through all records and display them as list items. But this time, we enhance things by adding a transition link to their detail page by passing their `id` to the `params` as shown above!
93-
9496
## Person detail page
95-
In the `app/matestack/demo/pages/persons/` directory, add a file called `show.rb` with the contents below:
97+
98+
Next we create the show page for a person. Therefore we create a file called `show.rb` alongside the `index.rb` inside `app/matestack/demo/pages/persons`.
9699

97100
```ruby
98101
class Demo::Pages::Persons::Show < Matestack::Ui::Page
99102

100103
def response
101-
transition path: :persons_path, text: 'Back to index'
104+
transition path: persons_path, text: 'All persons'
102105
heading size: 2, text: "Name: #{@person.first_name} #{@person.last_name}"
103106
paragraph text: "Role: #{@person.role}"
104107
end
105108

106109
end
107110
```
108111

109-
This is our detail page, featuring not only the person's name, but also their role. To make things easy for page visitors, there's also a "back" link to get back to our index page!
112+
The show page displays the persons firstname and lastname in a _h2_ tag and underneath the persons role in a _p_ tag. Above those information is a transition to the persons index page.
113+
114+
As you might see, we can access instance variables from controllers and rails helpers inside of our pages. This is also applicable for apps and components. We have access to everything we would have access to in a standard rails view.
110115

111116
## Further introduction: Page transitions
117+
112118
Now that we've used them a couple of times, let's focus on the `transition` component a bit longer:
113119

114120
When you want to change between different pages within the same `matestack` app, using a `transition` component gives you a neat advantage: After clicking the link, instead of doing a full page reload, only the page content within your app gets replaced - this leads to a better performance (faster page load) and a more app-like feeling for your users or page visitors!
@@ -118,31 +124,19 @@ For links that go outside your `matestack` app, require a full page reload or re
118124
To learn more, check out the [complete API documentation](docs/components/transition.md) for the `transition` component.
119125

120126
## Local testing
127+
121128
Run `rails s` and head over to [localhost:3000](http://localhost:3000/) to test the changes! You should be able to browse through the various persons in the database and switch between the different pages using the transition links.
122129

123130
## Saving the status quo
131+
124132
As usual, we want to commit the progress to Git. In the repo root, run
125133

126134
```sh
127135
git add . && git commit -m "Add index/show matestack pages for person model (incl. controller, routes), update demo matestack app"
128136
```
129137

130-
## Deployment
131-
After you've finished all your changes and commited them to Git, run
132-
133-
```sh
134-
git push heroku master
135-
```
136-
137-
to deploy your latest changes (unlike last time, no migrations are needed since the database schema remains unchanged). Check the results via
138-
139-
```sh
140-
heroku open
141-
```
142-
143-
and be proud of yourself - you're getting somewhere with this!
144-
145138
## Recap & outlook
146-
Our **person** model now has a dedicated index and show (=detail) page, and the pages within our `matestack` app are properly linked to each other.
147139

148-
Let's continue and add the necessary functionality for adding new persons and editing existing ones in the [next part of the series](/guides/essential/04_form_create_update_delete.md).
140+
Our **person** model now has a dedicated index and show page. The pages within our `matestack` app are properly linked to each other. We learned how we can access data and use rails helpers inside of pages, apps and components and how transitions in more detail work.
141+
142+
Let's continue and add the necessary functionality for adding new persons and editing existing ones in the [next part of the series](/guides/essential/04_form_create_update_delete.md).

0 commit comments

Comments
 (0)