Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 208
alexventuraio Avatar asked Sep 20 '16 04:09

alexventuraio


2 Answers

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.

like image 146
alexventuraio Avatar answered Sep 30 '22 10:09

alexventuraio


This question really is a challenge to my English :D

I googled the noun media, and it says that some purists insist that the word media, which is borrowed from Latin, is the plural form of medium, so from this point of view, Rails generator did the right job.

But in daily English, people simply use media as an uncountable noun or a collective noun. If you agree on that, then adjust the config/initializers/inflections.rb

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.uncountable %w( media )
end
like image 26
Aetherus Avatar answered Sep 30 '22 09:09

Aetherus