Routes 如何更改Rails路由助手以符合我们的意愿?
我使用以下命令生成了一个scaffold:Routes 如何更改Rails路由助手以符合我们的意愿?,routes,ruby-on-rails-3.2,helpers,Routes,Ruby On Rails 3.2,Helpers,我使用以下命令生成了一个scaffold: rails generate scaffold indice valeur:decimal date:date 我使用华丽的gem“”通过I18国际化将URL(路径)翻译成法语 我还使用我的config/route.rb将Indexes controller放入命名空间中(app/controller/catalogs/pub_indexes_controller.rb): namespace :catalogs do resources
rails generate scaffold indice valeur:decimal date:date
我使用华丽的gem“”通过I18国际化将URL(路径)翻译成法语
我还使用我的config/route.rb将Indexes controller放入命名空间中(app/controller/catalogs/pub_indexes_controller.rb):
namespace :catalogs do
resources :pub_indices
end
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'indice', 'indices'
end
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'indice', 'indices'
end
由于轨道弯曲,生成的路线不正确。它使用“索引”而不是“索引”表示单数,尽管我使用“索引”生成,我的模型称为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"}
所以我在谷歌上搜索了一下,发现了“拐点”这个词。。。
因此,我使用了我的config/route.rb中提供的技巧:
namespace :catalogs do
resources :pub_indices
end
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'indice', 'indices'
end
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拒绝接受使用如下“索引”的路由:新目录\u发布\u索引\u路径
相反,它接受了错误的形式:新目录\发布\索引\路径,尽管rake route的说法有所不同
谁能解释一下我做错了什么?(我希望Rails没有使用这种单数/复数形式。它会简单得多)
====更新=====
好的,我很快就回答了这个问题。经过更多的搜索,Il意识到我的“拐点”代码应该放在config/initializers/definctions.rb中:
namespace :catalogs do
resources :pub_indices
end
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'indice', 'indices'
end
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'indice', 'indices'
end
我只是解释一下。它不知道在哪里。因此,当上下文讨论config/route.rb时,我考虑将代码放入route.rb中,但这是错误的
官方文件缺乏对拐点的解释。真可惜^^
小心点,伙计们;-) 好的,正如我在更新中解释的那样 路由帮助程序语法可以通过使用config/initializers/deflocations.rb中的infelctor来更改 下面是一个例子