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

2

6

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?

Crawl answered 20/9, 2016 at 4:37 Comment(0)
C
8

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.

Crawl answered 20/9, 2016 at 4:37 Comment(0)
E
6

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
Elongate answered 20/9, 2016 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.