Javascript Rails控制器返回整个react应用程序
我有一个Rails API应用程序和一个独立的React应用程序 我想创建一个控制器,将整个应用程序返回给最终用户,并让react应用程序使用来自专用控制器的API,而不是创建两个服务器 我怎样才能干净利落呢 react应用程序树是:Javascript Rails控制器返回整个react应用程序,javascript,ruby-on-rails,ruby,reactjs,integration,Javascript,Ruby On Rails,Ruby,Reactjs,Integration,我有一个Rails API应用程序和一个独立的React应用程序 我想创建一个控制器,将整个应用程序返回给最终用户,并让react应用程序使用来自专用控制器的API,而不是创建两个服务器 我怎样才能干净利落呢 react应用程序树是: 公开的 favicon.ico index.html manifest.json src 资产/ 成分/ 观点/ App.css App.js index.css index.js registerServiceWorker.js 自述 package.
- 公开的
- favicon.ico
- index.html
- manifest.json
- src
- 资产/
- 成分/
- 观点/
- App.css
- App.js
- index.css
- index.js
- registerServiceWorker.js
- 自述
- package.josn
- package-lock.json
一般来说,您所要做的就是创建“Rails API”应用程序并使用控制器呈现json,然后使用React fetch/axios连接到您的Rails API,我无法直接向您介绍如何连接它们,这可能有点长,但这里有一个链接可以帮助您:
一般来说,您所要做的就是创建“Rails API”应用程序并使用控制器呈现json,然后使用React fetch/axios连接到您的Rails API,您的Rails API在这方面不是很有经验,但如果有帮助的话,这就是我在最近的项目中所做的:
- 使用/集成到Rails项目中
- app/config/routes.rb:
# all JSON requests goes here scope constraints: -> (request) { request.format == :json } do # all of your Rails API routes goes here end # all HTML requests goes here, # as your react-router should already be able to handle this scope constraints: -> (request) { request.format == :html } do # this matches ALL paths to bootstrapper action match '*path', to: 'application#bootstrapper' end root 'application#boostrapper'
def bootstrapper render template: 'layouts/bootstrapper' end
- app/controllers/application\u controller.rb:
# all JSON requests goes here scope constraints: -> (request) { request.format == :json } do # all of your Rails API routes goes here end # all HTML requests goes here, # as your react-router should already be able to handle this scope constraints: -> (request) { request.format == :html } do # this matches ALL paths to bootstrapper action match '*path', to: 'application#bootstrapper' end root 'application#boostrapper'
def bootstrapper render template: 'layouts/bootstrapper' end
- app/views/layouts/bootstrapper.html.erb
<!DOCTYPE html> <html lang="en"> <head> <!-- YOUR USUAL <head> HERE --> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %> </head> <body> <!-- DONT NEED THE <%= yield %> HERE --> <%= javascript_pack_tag 'your_react_pack' %> <%= stylesheet_pack_tag 'your_react_pack' %> </body> </html>
- 最后配置您的应用程序/javascript/packs/your_react_pack.js(这是您的react文件的入口点,因此您将
在此处导入
您的react应用程序)有关详细信息,请参阅
- 必须提供跨站点请求伪造(CSRF)保护!在Rails 5(或者也可能是4?)中,您只需使用
在JS中获取令牌,并在向Rails应用程序提交JSON请求时将其作为Rails.csrfToken()
头传递。确保您的应用程序/assets/javascripts/application.js中有'X-CSRF-token'
,以便能够使用/=require rails ujs
rails.csrfToken()
- 使用/集成到Rails项目中
- app/config/routes.rb:
# all JSON requests goes here scope constraints: -> (request) { request.format == :json } do # all of your Rails API routes goes here end # all HTML requests goes here, # as your react-router should already be able to handle this scope constraints: -> (request) { request.format == :html } do # this matches ALL paths to bootstrapper action match '*path', to: 'application#bootstrapper' end root 'application#boostrapper'
def bootstrapper render template: 'layouts/bootstrapper' end
- app/controllers/application\u controller.rb:
# all JSON requests goes here scope constraints: -> (request) { request.format == :json } do # all of your Rails API routes goes here end # all HTML requests goes here, # as your react-router should already be able to handle this scope constraints: -> (request) { request.format == :html } do # this matches ALL paths to bootstrapper action match '*path', to: 'application#bootstrapper' end root 'application#boostrapper'
def bootstrapper render template: 'layouts/bootstrapper' end
- app/views/layouts/bootstrapper.html.erb
<!DOCTYPE html> <html lang="en"> <head> <!-- YOUR USUAL <head> HERE --> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %> </head> <body> <!-- DONT NEED THE <%= yield %> HERE --> <%= javascript_pack_tag 'your_react_pack' %> <%= stylesheet_pack_tag 'your_react_pack' %> </body> </html>
- 最后配置您的应用程序/javascript/packs/your_react_pack.js(这是您的react文件的入口点,因此您将
在此处导入
您的react应用程序)有关详细信息,请参阅
- 必须提供跨站点请求伪造(CSRF)保护!在Rails 5(或者也可能是4?)中,您只需使用
在JS中获取令牌,并在向Rails应用程序提交JSON请求时将其作为Rails.csrfToken()
头传递。确保您的应用程序/assets/javascripts/application.js中有'X-CSRF-token'
,以便能够使用/=require rails ujs
rails.csrfToken()
- 在这方面不是很有经验,但如果有帮助的话,这就是我在最近的项目中所做的: