ember/rails - rails 路由中包含下划线的奇怪行为

ember/rails - Strange behavior in rails route containing an underscore

我正在玩一个使用 ember 前端并使用 jsonapi 资源构建 rails api 的测试应用程序。我在 rails 应用程序中的路线定义如下

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  jsonapi_resources :books
  jsonapi_resources :authors
  jsonapi_resources :publishing_houses
end

但是我注意到了一些奇怪的事情。如果我像这样转到 publishing_houses 的路径: http://localhost:3000/publishing_houses it says the route/page doesn't exist. However if I go to http://localhost:3000/publishing-houses 在出版社之间用破折号而不是下划线,我会得到我想要的响应。

问题出在我的 ember 应用程序中,我检查了控制台,它使用带有下划线的 url: http://localhost:3000/publishing_houses 请求数据,所以我没有得到我的 ember 请求中的任何数据。

这种行为有原因吗?还是我做错了什么?

我做了一些挖掘,似乎有路由格式的配置。试试这个

# config/initializers/jsonapi.rb
JSONAPI.configure do |config|
  config.route_format = :underscored_route
end

这应该将路由转换为“/publishing_houses”而不是 "publishing-houses",这将使它们与您正在使用的其他库兼容。