It seems pluralize()
only works within a view -- is there some way that my models can use pluralize()
too?
(I have methods in my model that return message strings for users that do not go to a view -- for example messages sent via SMS text message.)
Add this to your model:
This worked for me in rails 5.1 (see 2nd method, first method is calling it.)
YOu can add a method like this in your model
and call it in this way
My favorite way is to create a TextHelper in my app that provides these as class methods for use in my model:
app/helpers/text_helper.rb
app/models/any_model.rb
Including ActionView::Helpers::TextHelper in your models works, but you also litter up your model with lots of helper methods that don't need to be there.
It's also not nearly as clear where the pluralize method came from in your model. This method makes it explicit -
TextHelper.pluralize
.Finally, you won't have to add an include to every model that wants to pluralize something; you can just call it on TextHelper directly.
Rather than extend things, I just it like this:
Hope this helps someone else!