Skip to content

Commit 91d7fd1

Browse files
committed
Unify use of jbuilder with server rendering
* Change to use the same JSON for the server side load and the client side load of the comments. * Reuse one jbuilder template for the list and single view of comments. * Update the docs with how to do this.
1 parent 11cfbe6 commit 91d7fd1

File tree

6 files changed

+69
-8
lines changed

6 files changed

+69
-8
lines changed

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ You can see this tutorial live here: [http://reactrails.com/](http://reactrails.
4040

4141
In no particular order:
4242

43+
- Example of using the [react_on_rails](https://github.com/shakacode/react_on_rails)
4344
- Example of Rails 4.2 with ReactJs/Redux with Webpack and ES7.
4445
- Enable development of a JS client independently from Rails using Webpack Hot Module Reload. You can see this by starting the app and visiting http://localhost:3000
4546
- Easily enable use of npm modules with a Rails application.
@@ -257,6 +258,55 @@ npm install
257258
cd client && npm run build:client && npm run build:server
258259
```
259260

261+
# JBuilder Notes
262+
There's a bunch of gotchas with using [Jbuilder](https://github.com/rails/jbuilder) to create the
263+
string version of the props to be sent to the react_on_rails_gem:
264+
265+
See the notes in this the example code. The two critical things:
266+
267+
1. Use `render_to_string` to create string of JSON.
268+
2. Be sure to call `respond_to` afterwards.
269+
270+
app/controllers/pages_controller.rb
271+
272+
```ruby
273+
class PagesController < ApplicationController
274+
def index
275+
# NOTE: this could be an alternate syntax if you wanted to pass comments as a variable to a partial
276+
# @comments_json_string = render_to_string(partial: "/comments/comments.json.jbuilder", locals: { comments: Comment.all }, format: :json)
277+
@comments = Comment.all
278+
279+
# NOTE: @comments is used by the render_to_string call
280+
@comments_json_string = render_to_string("/comments/index.json.jbuilder")
281+
282+
# NOTE: It's CRITICAL to call respond_to after calling render_to_string, or else Rails will
283+
# not render the HTML version of the index page properly.
284+
respond_to do |format|
285+
format.html
286+
end
287+
end
288+
end
289+
```
290+
291+
### comments/_comment.json.jbuilder:
292+
293+
```ruby
294+
json.extract! comment, :id, :author, :text, :created_at, :updated_at
295+
```
296+
297+
### comments/index.json.jbuilder:
298+
299+
```ruby
300+
# Specify the partial, as well as the name of the variable used in the partial
301+
json.array! @comments, { partial: "comments/comment", as: :comment }
302+
```
303+
304+
### comments/show.json.jbuilder:
305+
306+
```ruby
307+
json.partial! 'comment', comment: @comment
308+
```
309+
260310
# Linting and Code Inspection
261311
## Running Lint and CI tasks
262312
* Default rake task runs tests and linting (yes, repeating this!) (see `ci.rake`)

app/controllers/pages_controller.rb

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
class PagesController < ApplicationController
22
def index
3+
# NOTE: this could be an alternate syntax if you wanted to pass comments as a variable to a partial
4+
# @comments_json_sting = render_to_string(partial: "/comments/comments.json.jbuilder",
5+
# locals: { comments: Comment.all }, format: :json)
36
@comments = Comment.all
7+
8+
# NOTE: @comments is used by the render_to_string call
9+
@comments_json_string = render_to_string("/comments/index.json.jbuilder")
10+
11+
# NOTE: It's CRITICAL to call respond_to after calling render_to_string, or else Rails will
12+
# not render the HTML version of the index page properly.
13+
respond_to do |format|
14+
format.html
15+
end
416
end
517
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.extract! comment, :id, :author, :text, :created_at, :updated_at
+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
json.array!(@comments) do |comment|
2-
json.extract! comment, :id, :author, :text
3-
json.url comment_url(comment, format: :json)
4-
end
1+
# Specify the partial, as well as the name of the variable used in the partial
2+
json.array! @comments, { partial: "comments/comment", as: :comment }

app/views/comments/show.json.jbuilder

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
json.extract! @comment, :id, :author, :text, :created_at, :updated_at
1+
json.partial! 'comment', comment: @comment

app/views/pages/index.html.erb

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<li>
44
If this work interests you and you're a developer or designer looking for full or part-time remote work: please
55
<%= link_to "click here",
6-
"http://forum.railsonmaui.com/t/railsonmaui-is-hiring-and-partnering-part-time-remote-is-ok/156" %>
6+
"http://forum.shakacode.com/t/railsonmaui-is-hiring-and-partnering-part-time-remote-is-ok/156/3" %>
77
and
88
<%= link_to "here",
9-
"http://forum.railsonmaui.com/t/railsonmaui-is-hiring-and-partnering-part-time-remote-is-ok/156/2" %>.
9+
"http://forum.shakacode.com/t/railsonmaui-is-hiring-and-partnering-part-time-remote-is-ok/156/2" %>.
1010
</li>
1111
<li>
1212
Please see the
@@ -16,4 +16,4 @@
1616
</li>
1717
</ul>
1818
<hr/>
19-
<%= react_component('App', @comments, generator_function: true, prerender: true) %>
19+
<%= react_component('App', @comments_json_string, generator_function: true, prerender: true) %>

0 commit comments

Comments
 (0)