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?
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")
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. ...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With