我使用以下命令生成了一个脚手架:
rails generate scaffold indice valeur:decimal date:date
我使用华丽的 gem“ rails-translate-routes ”将 URL(路径)翻译成 I18 国际化的法语。
我还使用我的config/route.rb将索引控制器放入命名空间(app/controller/catalogs/pub_indices_controller.rb):
namespace :catalogs do
resources :pub_indices
end
由于 Rails 变形,生成的路线不正确。它使用“index”而不是“indice”来表示单数,尽管我使用“indice”来生成并且我的模型被称为 pub_indice.rb :
catalogs_pub_indices_fr GET /catalogues/indices(.:format) catalogs/pub_indices#index {:locale=>"fr"}
catalogs_pub_indices_en GET /en/catalogs/pub_indices(.:format) catalogs/pub_indices#index {:locale=>"en"}
POST /catalogues/indices(.:format) catalogs/pub_indices#create {:locale=>"fr"}
POST /en/catalogs/pub_indices(.:format) catalogs/pub_indices#create {:locale=>"en"}
new_catalogs_pub_index_fr GET /catalogues/indices/nouveau(.:format) catalogs/pub_indices#new {:locale=>"fr"}
new_catalogs_pub_index_en GET /en/catalogs/pub_indices/new(.:format) catalogs/pub_indices#new {:locale=>"en"}
edit_catalogs_pub_index_fr GET /catalogues/indices/:id/modifier(.:format) catalogs/pub_indices#edit {:locale=>"fr"}
edit_catalogs_pub_index_en GET /en/catalogs/pub_indices/:id/edit(.:format) catalogs/pub_indices#edit {:locale=>"en"}
catalogs_pub_index_fr GET /catalogues/indices/:id(.:format) catalogs/pub_indices#show {:locale=>"fr"}
catalogs_pub_index_en GET /en/catalogs/pub_indices/:id(.:format) catalogs/pub_indices#show {:locale=>"en"}
PUT /catalogues/indices/:id(.:format) catalogs/pub_indices#update {:locale=>"fr"}
PUT /en/catalogs/pub_indices/:id(.:format) catalogs/pub_indices#update {:locale=>"en"}
DELETE /catalogues/indices/:id(.:format) catalogs/pub_indices#destroy {:locale=>"fr"}
DELETE /en/catalogs/pub_indices/:id(.:format) catalogs/pub_indices#destroy {:locale=>"en"}
所以我用谷歌搜索了一下,发现了“inflector”这个词......所以我使用了官方文档在我的config/route.rb中提供的技巧:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'indice', 'indices'
end
路线助手看起来还不错:
catalogs_pub_indices_fr GET /catalogues/indices(.:format) catalogs/pub_indices#index {:locale=>"fr"}
catalogs_pub_indices_en GET /en/catalogs/pub_indices(.:format) catalogs/pub_indices#index {:locale=>"en"}
POST /catalogues/indices(.:format) catalogs/pub_indices#create {:locale=>"fr"}
POST /en/catalogs/pub_indices(.:format) catalogs/pub_indices#create {:locale=>"en"}
new_catalogs_pub_indice_fr GET /catalogues/indices/nouveau(.:format) catalogs/pub_indices#new {:locale=>"fr"}
new_catalogs_pub_indice_en GET /en/catalogs/pub_indices/new(.:format) catalogs/pub_indices#new {:locale=>"en"}
edit_catalogs_pub_indice_fr GET /catalogues/indices/:id/modifier(.:format) catalogs/pub_indices#edit {:locale=>"fr"}
edit_catalogs_pub_indice_en GET /en/catalogs/pub_indices/:id/edit(.:format) catalogs/pub_indices#edit {:locale=>"en"}
catalogs_pub_indice_fr GET /catalogues/indices/:id(.:format) catalogs/pub_indices#show {:locale=>"fr"}
catalogs_pub_indice_en GET /en/catalogs/pub_indices/:id(.:format) catalogs/pub_indices#show {:locale=>"en"}
PUT /catalogues/indices/:id(.:format) catalogs/pub_indices#update {:locale=>"fr"}
PUT /en/catalogs/pub_indices/:id(.:format) catalogs/pub_indices#update {:locale=>"en"}
DELETE /catalogues/indices/:id(.:format) catalogs/pub_indices#destroy {:locale=>"fr"}
DELETE /en/catalogs/pub_indices/:id(.:format) catalogs/pub_indices#destroy {:locale=>"en"}
但是 Rails 拒绝接受这样的“索引”路由:new_catalogs_pub_indice_path
相反,它接受了不正确的形式:new_catalogs_pub_index_path尽管 rake route 说的是不同的东西。
谁能解释我做错了什么?(我希望 Rails 没有使用这种单数/复数形式。这会简单得多sigh)
===== 更新 =====
好的,我要快速回答这个问题。经过一番搜索,我意识到我的“变形器”代码应该放在config/initializers/inflections.rb中:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'indice', 'indices'
end
官方文档只是解释了如何。它没有告诉你在哪里。因此,当上下文谈论 config/route.rb 时,我考虑将我的代码放入 route.rb 但这是错误的。
官方文档缺乏关于变形器的解释。真可惜^^要小心,伙计们;-)