I generated a scaffold with following command :
rails generate scaffold indice valeur:decimal date:date
I use the gorgeous gem “rails-translate-routes” to translate URLs(path) to french with I18 internationalization.
I as well put the indices controller into a namespace (app/controller/catalogs/pub_indices_controller.rb) with my config/route.rb :
namespace :catalogs do
resources :pub_indices
end
The resulting routes are incorrect because of Rails inflection. It use “index” instead of “indice” for singular, though I use “indice” to generate and my model is called 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"}
So I googled a bit and discover the word “inflector”…
So I used the trick provided by official doc in my config/route.rb :
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'indice', 'indices'
end
And the route helpers look alright :
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"}
But Rails refuse to accept routes using “indice” like this : new_catalogs_pub_indice_path
Instead it accept the incorrect form : new_catalogs_pub_index_path though rake route is saying something different.
Could anyone explain me what am I doing wrong ? (I wish Rails didn’t used this singular/plural form. It would have been so much simpler sigh)
===== UPDATE =====
Ok, I was to quick on this question. After some more search, Il realized my “inflector” code should rather be placed into config/initializers/inflections.rb :
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'indice', 'indices'
end
The official doc just explain how. It doesn’t tell where. So as the context was talking about config/route.rb I thought about puting my code into route.rb but that is wrong.
Official doc is lacking explanation about inflector. That’s a shame ^^
Be carefull guys 😉
Ok, as I explained in my update.
route helpers syntax may be changed by using infelctor in config/initializers/inflections.rb.
The official doc shows an example.