【问题标题】:How can I pass a path to a statically served react app from rails api如何从 rails api 将路径传递给静态服务的反应应用程序
【发布时间】:2019-12-02 18:42:21
【问题描述】:

我有以下设置:

create-react-app 前端与 react-router 捆绑到静态资产中。
这个静态资产从我的rails-api 项目的public 文件夹作为静态文件提供。 React 应用程序使用 rails-api 作为后端。
当我转到 / 路由时,会提供 react 应用程序,我可以导航我的应用程序。

如何将/admin 之类的任何子路径传递给 react-router? 我已经有一条包罗万象的路线 config/routes.rb

get '*path', to: "application#fallback_index_html", constraints: lambda { |req|
    req.path.exclude? 'rails/active_storage'
  }

而且我可以在控制器中获取相关的子路径:

  def fallback_index_html
    path = request.parameters['path']
    render file: 'public/index.html'
  end

但我不知道如何为 react 应用提供服务将路径传递给它。 有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails reactjs react-router create-react-app rails-api


    【解决方案1】:

    原来,问题在于我的ApplicationController 是从ActionController::API 派生的,而不是ActionController::Base。使用 Base 控制器,它就像一个魅力。 另见https://stackoverflow.com/a/43912670/526426

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2016-09-19
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      相关资源
      最近更新 更多