@@ -25,20 +25,38 @@ Lets begin by adding a route and a controller to an app.
25
25
=== "` greet_controller.rb ` "
26
26
in ` app/controllers/greet_controller.rb `
27
27
28
+ !!! tip Enable jsx rendering defaults
29
+ `use_jsx_rendering_defaults` enables Rails to look for `.jsx` files and
30
+ pairs with `.props` files. For example:
31
+
32
+ ```
33
+ app/views
34
+ application/
35
+ superglue.html.erb
36
+ about/
37
+ index.jsx
38
+ users/
39
+ index.jsx
40
+ index.json.props
41
+ ```
42
+
28
43
```ruby
29
44
class GreetController < ApplicationController
45
+ before_action :use_jsx_rendering_defaults
46
+
30
47
def show
31
48
end
32
49
end
33
50
```
34
51
35
52
### Add the views
36
53
37
- Next lets add the following views. Here we're splitting the usual ` show.html.erb ` into 3 parts:
38
-
54
+ Next lets add the following views.
39
55
- ` app/views/greet/show.json.props `
40
- - ` app/views/greet/show.js `
41
- - ` app/views/greet/show.html.erb `
56
+ - ` app/views/greet/show.jsx `
57
+
58
+ The Superglue installation generator also adds a ` application/superglue.html.erb ` , which
59
+ will be used as the default html template for every controller action.
42
60
43
61
Click the tabs below to see the contents:
44
62
@@ -72,7 +90,7 @@ Click the tabs below to see the contents:
72
90
body,
73
91
footer
74
92
} = useContent();
75
-
93
+
76
94
const {greet} = body
77
95
78
96
return (
@@ -84,22 +102,15 @@ Click the tabs below to see the contents:
84
102
}
85
103
```
86
104
87
- === "3. ` show.html.erb ` "
88
-
89
- !!! info
90
- This file is usually generated by a scaffold and stays exactly the same
91
- regardless if its `index.html.erb`, `show.html.erb`, `edit.html.erb`, etc.
105
+ === "3. ` application/superglue.html.erb ` "
92
106
93
107
94
108
```ruby
95
- <% initial_state = controller.render_to_string(formats: [:json], locals: local_assigns, layout: true) %>
96
-
97
109
<script type="text/javascript">
98
- window.SUPERGLUE_INITIAL_PAGE_STATE=<%= initial_state.html_safe %>;<%# erblint:disable ErbSafety %>
110
+ window.SUPERGLUE_INITIAL_PAGE_STATE=<%= render_props %>;<%# erblint:disable ErbSafety %>
99
111
</script>
100
112
101
113
<div id="app"></div>
102
-
103
114
```
104
115
105
116
This file renders `show.json.props`, injects it globally as the initial
@@ -108,10 +119,10 @@ Click the tabs below to see the contents:
108
119
109
120
### Connect the dots
110
121
111
- The json [ payload] that gets rendered into ` show.html.erb ` contains an the
112
- ` componentIdentifier ` . We're going to use the ` componentIdentifier ` to tie
113
- ` show.json.props ` to ` show.js ` so superglue knows which component to render
114
- with which response by modifying ` app/javascript/page_to_page_mapping.js ` .
122
+ The json [ payload] that gets injected contains a ` componentIdentifier ` . We're
123
+ going to use the ` componentIdentifier ` to tie ` show.json.props ` to ` show.js ` so
124
+ superglue knows which component to render with which response by modifying
125
+ ` app/javascript/page_to_page_mapping.js ` .
115
126
116
127
[ payload ] : page-response.md
117
128
@@ -121,7 +132,7 @@ with which response by modifying `app/javascript/page_to_page_mapping.js`.
121
132
gets rendered. In our case: http://localhost:3000/greet.json
122
133
123
134
**Vite Users** This step can be entirely optional if you're using Vite. See
124
- the [recipe](/superglue/ recipes/vite/ ) for more information.
135
+ the [recipe](recipes/vite.md ) for more information.
125
136
126
137
=== "1. Example ` greet.json ` "
127
138
The layout for ` show.json.props ` is located at ` app/views/layouts/application.json.props ` . It
0 commit comments