首页 > 解决方案 > 如何从 rails api 将路径传递给静态服务的反应应用程序

问题描述

我有以下设置:

带有它的create-react-app前端react-router被捆绑到静态资产中。此静态资产作为静态文件
从我rails-api的项目文件夹中提供。publicreact 应用程序使用rails-apias 后端。
当我去/路由时,会提供反应应用程序,我可以浏览我的应用程序。

如何将任何子路径传递/admin给反应路由器?我已经有一条包罗万象的路线 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

但我不知道如何为反应应用程序提供服务并将路径传递给它。有任何想法吗?

标签: ruby-on-railsreactjsreact-routercreate-react-apprails-api

解决方案


事实证明,问题在于 myApplicationController源自ActionController::API而不是ActionController::Base. 使用 Base 控制器,它就像一个魅力。另请参阅https://stackoverflow.com/a/43912670/526426


推荐阅读