Javascript Rails控制器返回整个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.

我有一个Rails API应用程序和一个独立的React应用程序

我想创建一个控制器,将整个应用程序返回给最终用户,并让react应用程序使用来自专用控制器的API,而不是创建两个服务器

我怎样才能干净利落呢

react应用程序树是:

  • 公开的
    • favicon.ico
    • index.html
    • manifest.json
  • src
    • 资产/
    • 成分/
    • 观点/
    • App.css
    • App.js
    • index.css
    • index.js
    • registerServiceWorker.js
  • 自述
  • package.josn
  • package-lock.json

我无法直接向您介绍如何连接它们,可能会有点长,但这里有一个链接可以帮助您实现连接:

  • 此链接介绍了“创建react应用程序”,这是您创建react应用程序和Rails API所做的工作

  • 一般来说,您所要做的就是创建“Rails API”应用程序并使用控制器呈现json,然后使用React fetch/axios连接到您的Rails API,我无法直接向您介绍如何连接它们,这可能有点长,但这里有一个链接可以帮助您:

  • 此链接介绍了“创建react应用程序”,这是您创建react应用程序和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?)中,您只需使用
      Rails.csrfToken()
      在JS中获取令牌,并在向Rails应用程序提交JSON请求时将其作为
      'X-CSRF-token'
      头传递。确保您的应用程序/assets/javascripts/application.js中有
      /=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?)中,您只需使用
          Rails.csrfToken()
          在JS中获取令牌,并在向Rails应用程序提交JSON请求时将其作为
          'X-CSRF-token'
          头传递。确保您的应用程序/assets/javascripts/application.js中有
          /=require rails ujs
          ,以便能够使用
          rails.csrfToken()

        您可能希望在资产管道中捆绑JS。有关于如何进行此操作的教程。请在路线中进行设置。根路径应提供客户端应用程序。剩下的可以是API。您可能希望在资产管道中捆绑JS。有关于如何进行此操作的教程。请在路线中进行设置。根路径应提供客户端应用程序。其余的可以是API。