19

I have a model named ActiveDns. When I run

rails g scaffold_controller ActiveDns

I get the message

Plural version of the model detected, using singularized version. Override with --force-plural.

Now, the controller and views are generated pretending that the singular is ActiveDn and the plural is ActiveDns, and I get silly stuff like link_to new_dn_path. The --force-plural argument doesn't appear to fix this:

rails g scaffold_controller ActiveDns --force-plural

still results in controllers using variables named @active_dn and views using new_dn_path, with rails 3.2.3. I am removing files between tries using rails d scaffold_controller ActiveDns.

What's the right way to do this?

3 Answers3

18

What's the right way to do this?

I use inflections to document uncountable entities.

config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable "ActiveDns"
end

Then you get:

$ rails g scaffold_controller ActiveDns
      create  app/controllers/active_dns_controller.rb
      invoke  erb
      create    app/views/active_dns
      create    app/views/active_dns/index.html.erb
      create    app/views/active_dns/edit.html.erb
      create    app/views/active_dns/show.html.erb
      create    app/views/active_dns/new.html.erb
      create    app/views/active_dns/_form.html.erb
      invoke  test_unit
      create    test/functional/active_dns_controller_test.rb
      invoke  helper
      create    app/helpers/active_dns_helper.rb
      invoke    test_unit
      create      test/unit/helpers/active_dns_helper_test.rb

Is this what you wanted?

nurettin
  • 11,090
  • 5
  • 65
  • 85
16

I tested with rails-3.2 (I guess it should work with rails-3.x)

Open your config/initializers/inflections.rb and add a rule:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'dns', 'dnses'
end

And generate the controller

rails g scaffold_controller ActiveDns

And add routes to your config/routes.rb file

resources :active_dnses

Then you should see:

$ rake routes

   active_dnses GET    /active_dnses(.:format)          active_dnses#index
                POST   /active_dnses(.:format)          active_dnses#create
 new_active_dns GET    /active_dnses/new(.:format)      active_dnses#new
edit_active_dns GET    /active_dnses/:id/edit(.:format) active_dnses#edit
     active_dns GET    /active_dnses/:id(.:format)      active_dnses#show
                PUT    /active_dnses/:id(.:format)      active_dnses#update
                DELETE /active_dnses/:id(.:format)      active_dnses#destroy
Jacob Dam
  • 2,278
  • 18
  • 18
  • Works fine for me with rails 4.2.1 – Joey Hoang Jun 26 '15 at 18:19
  • had the same issue with "status" under rails 5 – Simon the Salmon Apr 27 '18 at 08:04
  • This solution almost worked for me, with exception of route helper methods. Alhought `rake routes` begaves correctly, the method `new_active_dns_path` does not exist, only `new_active_dnses_path` does, which is strange. Also the method `form_for(@active_dns)` keeps pluralizing in a wrong way – gorn May 29 '18 at 17:14
0

I want to share about how I solve my problem.

I have an MVC with a name of Pacman under Data_Warehouse.

Where Pacman is a proper noun. (Cannot be Pacmen with an e, but should be Pacmans with an s)

And Data_Warehouse is like a platform name. (Could be anything, in this case its DataWarehouse::Pacman)

So, I have

Models name - models/data_warehouse/pacman.rb

Views name - views/data_warehouse/pacmans/index.slim

Controller name - controller/data_warehouse/pacmans_controller.rb

The problem is, Rails read this path

data_warehouse_pacmans_path

as

data_warehouse_pacmen_path

because of pluralization.

So

I solved this by adding

ActiveSupport::Inflector.inflections do |inflect|
   inflect.uncountable %w( pacmans )
end

to the inflections.rb file in Rails.

Hope this would helps

Adrian Eranzi
  • 35
  • 2
  • 7
  • Welcome to the site. Please edit the question with details that is only relevant to the question you ask (avoid describing your details in the post). You can always use the edit function to update your question. – EmJ Apr 16 '19 at 04:37