Question

I have a need to define an inflection for the word 'chassis' where the same word defines both singular and plural and I am really struggling with this. I thought I was there with the initialize/inflections.rb definition

 ActiveSupport::Inflector.inflections do |inflect|
#   inflect.plural /^(ox)$/i, '\1en'
#   inflect.singular /^(ox)en/i, '\1'
#   inflect.irregular 'person', 'people'
#   inflect.uncountable %w( fish sheep )
    inflect.uncountable(/.*chassis.*/i)
 end

Taking note of the example inflect.uncountable %w( fish sheep ) I tried to use inflect.uncountable %w( chassis ) when I first set up the scaffolding but that didn't work at all well as it didn't take into account leading parts in paths and caused problems with relationships and other tables like car_chassis and chassis_lookup.

Having looked at various solutions provided as answers to similar questions in Stack Overflow I eventually came up with inflect.uncountable(/.*chassis.*/i) Which seemed to take care of scaffolding but I'm having an issue with routes where <%= link_to "Chassis", admin_chassis_url%> gives me a no route for the show action error.

ActionController::RoutingError - No route matches {:action=>"show", :controller=>"admin/chassis"}

Which makes sense as I want the index action so I'm not passing an object to the path but Rails is obviously thinking I am requesting the show action

The other examples for regex's

#   inflect.plural /^(ox)$/i, '\1en'
#   inflect.singular /^(ox)en/i, '\1'

are just complete gobbledygook to me and learning regular expressions needs a lifetime of learning that I neither have the inclination or the sanity to get my head round and the rails documentation http://api.rubyonrails.org/classes/ActiveSupport/Inflector/Inflections.html on inflections is quite frankly pathetic.

I obviously haven't got the inflection right. Can anyone give me a complete solution as to exactly how I should define an inflection for the word "chassis" for me and for others please as none of the previous answers I've found provide a complete and proper solution

Was it helpful?

Solution

Your inflection seem to be correct. Check what 'rake routes' tell you. In my case it was smart enough to detect that plural and single form of chassis was the same, so it generated admin_chassis_index instead of just admin_chassis for the #index action. Probably, the same is true for you. This is what I did:

In config/routes.rb

namespace :admin do
  resources :chassis
end

Running 'rake routes' gives (note the first path):

admin_chassis_index GET    /admin/chassis(.:format)          admin/chassis#index
                    POST   /admin/chassis(.:format)          admin/chassis#create
  new_admin_chassis GET    /admin/chassis/new(.:format)      admin/chassis#new
 edit_admin_chassis GET    /admin/chassis/:id/edit(.:format) admin/chassis#edit
      admin_chassis GET    /admin/chassis/:id(.:format)      admin/chassis#show
                    PUT    /admin/chassis/:id(.:format)      admin/chassis#update
                    DELETE /admin/chassis/:id(.:format)      admin/chassis#destroy

So, for #index I'd need to call:

<%= link_to "Chassis", admin_chassis_index_url%>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top