我已经有返回 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
和/api/v1/user
URL(例如,不向我自己的服务器发出 HTTP GET 请求)?