Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined method 'pluralize' for #<Controller>

Not sure why this has decided to stop working.

customers_controller.rb

redirect_to customers_url,
            notice: pluralize(@imported_customers.size, "customer") + " imported!"

And I'm getting the error:

NoMethodError: undefined method 'pluralize' for #CustomersController:0x007f3ca8378a20

Any idea where to start looking?

like image 373
Wes Foster Avatar asked Nov 14 '15 01:11

Wes Foster


3 Answers

If you don't want to use view helpers, then you can use String#pluralize:

"customer".pluralize(@imported_customers.size)

If you want to use view helpers then you should include the respective helper as another answers or just use ActionView::Rendering#view_context:

view_context.pluralize(@imported_customers.size, "customer")
like image 179
Aguardientico Avatar answered Nov 11 '22 04:11

Aguardientico


By default, the pluralize method is only made available in your views. To use it in a controller, put this at the top of your controller class:

include ActionView::Helpers::TextHelper

like

# app/controllers/cutomers_controller.rb

class CustomersController < ApplicationController
  include ActionView::Helpers::TextHelper

  def index
  etc. ...
like image 33
David Runger Avatar answered Nov 11 '22 06:11

David Runger


You can call pluralize helper with:

ActionController::Base.helpers.pluralize(@imported_customers.size, "customer") + " imported!"

or

# app/controllers/cutomers_controller.rb

class CustomersController < ApplicationController
  include ActionView::Helpers::TextHelper
like image 13
akbarbin Avatar answered Nov 11 '22 04:11

akbarbin