Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use "number_to_currency" inside a controller?

I a, building a dropdown menu in a form that displays a list of available options and their prices in this form: "service - price". My problem, however, is that it doesn't look like I can use number_to_currency inside my controller. Is there another way to achieve the same effect, or to access number_to_currency from my controller? Here is my initial effort:

@levels = []
DistributorLevel.all.each do |d|
  price = (d.price > 0) ? number_to_currency(d.price) : "Free"
  @levels << ["#{d.name} - #{price}", d.id]
end
like image 421
drewwyatt Avatar asked Apr 22 '14 01:04

drewwyatt


2 Answers

Yes, you could do with:

view_context.number_to_currency(d.price) 

or

ActionController::Base.helpers.number_to_currency(d.price) 
like image 96
xdazz Avatar answered Oct 31 '22 22:10

xdazz


Try:

include ActionView::Helpers::NumberHelper
like image 34
henrique_ms Avatar answered Oct 31 '22 22:10

henrique_ms