JSON 応答を返す Rails API コントローラーが既にあります。フロントエンド Javascript (およびモバイル アプリ) で値をレンダリングするために使用されます。
ここで、ReactJS を使用してこれらの値を事前にレンダリングしたいと思います。
#app/controllers/api/v1/products_controller.rb
module API
module V1
class ProductsController < ApplicationController
def index
@products = Product.all #this could be acomplex multi-line statements.
#rendered in api/v1/products/index.json.jbuilder
end
end
end
end
#app/controllers/products_controller.rb
class ProductsController < ApplicationController
def index
#How to do this efficiently?
@products_json = #Call to internal /api/v1/products/index for prerender purpose.
@user_json = #Call to internal /api/v1/user/show for prerender purpose.
end
end
#app/views/products/index.html.erb
<%= react_component('ProductsList', @products_json, {prerender: true}) %>
<%= react_component('UserProfile', @user_json, {prerender: true}) %>
/api/v1/products
内部およびURL を効率的に呼び出すにはどうすればよいですか/api/v1/user
(たとえば、自分のサーバーに HTTP GET 要求を行わずに)。