-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathpages_controller.rb
55 lines (45 loc) · 1.77 KB
/
pages_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
class PagesController < ApplicationController
include ReactOnRails::Controller
before_action :set_comments
def index
# NOTE: The below notes apply if you want to set the value of the props in the controller, as
# compared to the view. However, it's more convenient to use Jbuilder from the view. See
# app/views/pages/index.html.erb:20
#
# <%= react_component('App', props: render(template: "/comments/index.json.jbuilder"),
# prerender: true) %>
#
#
# NOTE: this could be an alternate syntax if you wanted to pass comments as a variable to a partial
# @comments_json_sting = render_to_string(partial: "/comments/comments.json.jbuilder",
# locals: { comments: Comment.all }, format: :json)
# NOTE: @comments is used by the render_to_string call
# @comments_json_string = render_to_string("/comments/index.json.jbuilder")
# NOTE: It's CRITICAL to call respond_to after calling render_to_string, or else Rails will
# not render the HTML version of the index page properly. (not a problem if you do this in the view)
# respond_to do |format|
# format.html
# end
redux_store("routerCommentsStore", props: comments_json_string)
render_html
end
# Declaring no_router and simple to indicate we have views for them
def no_router
redux_store("commentsStore", props: comments_json_string)
render_html
end
def simple; end
def rescript; end
private
def set_comments
@comments = Comment.all.order("id DESC")
end
def comments_json_string
render_to_string(template: "/comments/index",
locals: { comments: Comment.all }, formats: :json)
end
def render_html
respond_to(&:html)
end
end