ruby-on-rails - 如何从 rails api 将路径传递给静态服务的反应应用程序
问题描述
我有以下设置:
带有它的create-react-app
前端react-router
被捆绑到静态资产中。此静态资产作为静态文件
从我rails-api
的项目文件夹中提供。public
react 应用程序使用rails-api
as 后端。
当我去/
路由时,会提供反应应用程序,我可以浏览我的应用程序。
如何将任何子路径传递/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
但我不知道如何为反应应用程序提供服务并将路径传递给它。有任何想法吗?
解决方案
事实证明,问题在于 myApplicationController
源自ActionController::API
而不是ActionController::Base
. 使用 Base 控制器,它就像一个魅力。另请参阅https://stackoverflow.com/a/43912670/526426
推荐阅读
- php - 用 php rand() 数字填充 html 表
- python - 我们如何得到这个按位运算的答案?
- google-chrome-extension - 有没有办法从后台脚本获取浏览器信息,包括版本号?
- python - python - 属性 x 是一个属性对象,它为这个私有变量提供接口
- javascript - 用 jest 测试回调函数
- ios - 如何从来自后端的 mp3 文件 URL 获取元数据以显示 ArtWork Image?
- scala - 火花迭代编程 - 退出条件而不启动作业
- c# - 使用语句连接未关闭异常
- grafana - 如何过滤 label_values(label) 的结果以获取与正则表达式匹配的标签列表?
- php - 我想回应不可编辑的博客文章