Skip to content

Commit e00b606

Browse files
committed
Simple rendering of the Jbuilder
by rendering Jbuilder template in the view
1 parent 91d7fd1 commit e00b606

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

README.md

+13-22
Original file line numberDiff line numberDiff line change
@@ -260,33 +260,24 @@ cd client && npm run build:client && npm run build:server
260260

261261
# JBuilder Notes
262262
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:
263+
string version of the props to be sent to the react_on_rails_gem. The main thing is that if you
264+
follow the example and call Jbuilder like this, you don't run into a number of issues.
264265

265-
See the notes in this the example code. The two critical things:
266+
```erb
267+
<%= react_component('App', render(template: "/comments/index.json.jbuilder"),
268+
generator_function: true, prerender: true) %>
269+
```
270+
271+
However, if you try to set the value of the JSON string inside of the controller, then you will
272+
run into several issues with rendering the Jbuilder template from the controller.
273+
See the notes in this the example code for app/controllers/pages_controller.rb.
274+
275+
The two critical things:
266276

267277
1. Use `render_to_string` to create string of JSON.
268278
2. Be sure to call `respond_to` afterwards.
269279

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-
```
280+
Here's the samples of Jbuilder that we use:
290281

291282
### comments/_comment.json.jbuilder:
292283

app/controllers/pages_controller.rb

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
class PagesController < ApplicationController
22
def index
3+
@comments = Comment.all
4+
5+
# NOTE: The below notes apply if you want to set the value of the props in the controller, as
6+
# compared to he view. However, it's more convenient to use Jbuilder from the view. See
7+
# app/views/pages/index.html.erb:20
8+
#
9+
# <%= react_component('App', render(template: "/comments/index.json.jbuilder"),
10+
# generator_function: true, prerender: true) %>
11+
#
12+
#
313
# NOTE: this could be an alternate syntax if you wanted to pass comments as a variable to a partial
414
# @comments_json_sting = render_to_string(partial: "/comments/comments.json.jbuilder",
515
# locals: { comments: Comment.all }, format: :json)
6-
@comments = Comment.all
7-
816
# NOTE: @comments is used by the render_to_string call
9-
@comments_json_string = render_to_string("/comments/index.json.jbuilder")
10-
17+
# @comments_json_string = render_to_string("/comments/index.json.jbuilder")
1118
# 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
19+
# not render the HTML version of the index page properly. (not a problem if you do this in the view)
20+
# respond_to do |format|
21+
# format.html
22+
# end
1623
end
1724
end

app/views/pages/index.html.erb

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
</li>
1717
</ul>
1818
<hr/>
19-
<%= react_component('App', @comments_json_string, generator_function: true, prerender: true) %>
19+
20+
<%= react_component('App', render(template: "/comments/index.json.jbuilder"),
21+
generator_function: true, prerender: true) %>

0 commit comments

Comments
 (0)