@@ -260,33 +260,24 @@ cd client && npm run build:client && npm run build:server
260
260
261
261
# JBuilder Notes
262
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:
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.
264
265
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:
266
276
267
277
1 . Use ` render_to_string ` to create string of JSON.
268
278
2 . Be sure to call ` respond_to ` afterwards.
269
279
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:
290
281
291
282
### comments/_ comment.json.jbuilder:
292
283
0 commit comments