Search code examples
rubyruby-on-rails-5

Pluralizations and Singularizations (Inflections) for "Media/Medium" in Rails 5


I'm working with Rails 5 an I just have created a Media model using scaffold tool.

rails g scaffold media name:string

And I got different names and routes and view etc...

enter image description here

It was not right pluralising, so when I rake routes I got medium unexpected routes and because of that I got different problems in the views.

enter image description here

When I try to use <%= form_for @media do .. I got complain about no method media_index_path.

How can I get it fixed and working well?


Solution

  • Searching all day about this and being a little bit more curious when I run the very first scaffold it recommended to use something called inflections which basically is in charge of the pluralising and singularising words for class names:

    enter image description here

    What I did to get it fixed was to use inflections in the next way:

    1. Delete the model I just have created.

      rails d scaffold Media
      
    2. Edit config/initializers/inflections.rb with:

      ActiveSupport::Inflector.inflections(:en) do |inflect|
          # Here you can put the singular and plural form you expect
          inflect.irregular 'media', 'medias'
      end
      
    3. Now execute the scaffold again:

      rails g scaffold Media
      

    Now you must have everything in the way you expected. Because you have overwritten the Pluralizations and Singularizations (Inflections) in Ruby on Rails.

    I hope it could be useful.