我用以下命令生成了一个脚手架:
rails generate scaffold indice valeur:decimal date:date我使用华丽的宝石"rails.翻译.路线“将I18国际化的URL(Path)翻译成法语。
我还将索引控制器与我的config/route.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拒绝接受像这样使用"indice“的路由:new_catalogs_pub_indice_path
相反,它接受不正确的形式:new_catalogs_pub_index_path,尽管rake路由是在说一些不同的东西。
有人能解释我做错了什么吗?(我希望Rails没有使用这种单数/复数形式。这将是如此简单的叹息)
=====更新=====
好吧,我想快点回答这个问题。经过进一步的搜索,Il意识到我的"inflector“代码应该放在config/initializers/inflections.rb中:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'indice', 'indices'
end官方医生只是解释了怎么做。它不知道在哪里。因此,当上下文讨论config/route.rb时,我考虑将代码插入route.rb,但这是错误的。
官方医生对inflector缺乏解释。太可惜了,小心点吧;-)
发布于 2012-12-04 15:45:20
好的,正如我在更新中所解释的。
在config/initializers/inflections.rb.中使用infelctor可以更改路由帮助程序的语法
官方医生展示了一个示例。
https://stackoverflow.com/questions/13659513
复制相似问题