Skip to content

Commit b71b1c9

Browse files
author
Gabe Scholz
committed
Create render_react_component controller helper
1 parent b0dd8c3 commit b71b1c9

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

lib/react/rails.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require 'react/rails/railtie'
22
require 'react/rails/engine'
3+
require 'react/rails/controller_helper'
34
require 'react/rails/view_helper'

lib/react/rails/controller_helper.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module React
2+
module Rails
3+
module ControllerHelper
4+
5+
# Render a React component as the view instead of
6+
# a template from the app/views folder.
7+
#
8+
def render_react_component(*args, &block)
9+
current_layout = _layout.virtual_path
10+
@__react_component_args = args
11+
@__react_component_block = block
12+
13+
render inline: '<%= react_component(*@__react_component_args, &@__react_component_block) %>', layout: current_layout
14+
end
15+
16+
end
17+
end
18+
end
19+
20+
ActionController::Base.send(:include, React::Rails::ControllerHelper) if defined?(Rails)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class ServerController < ApplicationController
22
def show
33
@todos = %w{todo1 todo2 todo3}
4+
render_react_component "TodoList", {:todos => @todos}, :prerender => true
45
end
56
end

test/dummy/app/views/server/show.html.erb

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)