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/03_index_show_transition.md
+47-53Lines changed: 47 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,18 +2,29 @@
2
2
Welcome to the third part of the 10-step-guide of setting up a working Rails CRUD app with `matestack-ui-core`!
3
3
4
4
## 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.
6
6
7
7
In this guide, we will
8
-
- add proper page to display all the persons in our database
9
8
- 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
11
10
12
11
## 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).
14
13
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.
17
28
18
29
```ruby
19
30
classPersonsController < ApplicationController
@@ -31,84 +42,79 @@ class PersonsController < ApplicationController
31
42
end
32
43
```
33
44
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
-
44
45
## 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.
46
47
47
48
```ruby
48
49
classDemo::App < Matestack::Ui::App
49
50
50
51
defresponse
52
+
nav do
53
+
transition path: root_path, text:'Persons'
54
+
end
51
55
header do
52
56
heading size:1, text:'Demo App'
53
57
end
54
58
main do
55
-
page_content
59
+
yield_page
60
+
end
61
+
footer do
56
62
hr
57
-
transition path::persons_path, text:'All persons'
63
+
small text:'These guides are provided by matestack'
58
64
end
59
65
end
60
66
61
67
end
62
68
```
63
69
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
-
66
70
## 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.
68
71
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.
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
-
94
96
## 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`.
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.
110
115
111
116
## Further introduction: Page transitions
117
+
112
118
Now that we've used them a couple of times, let's focus on the `transition` component a bit longer:
113
119
114
120
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
118
124
To learn more, check out the [complete API documentation](docs/components/transition.md) for the `transition` component.
119
125
120
126
## Local testing
127
+
121
128
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.
122
129
123
130
## Saving the status quo
131
+
124
132
As usual, we want to commit the progress to Git. In the repo root, run
125
133
126
134
```sh
127
135
git add .&& git commit -m "Add index/show matestack pages for person model (incl. controller, routes), update demo matestack app"
128
136
```
129
137
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
-
145
138
## 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.
147
139
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