I would like to format a number in a controller before inserting it into a string. But the function number_with_delimiter() does not work in a controller. I need the string to send to a javascript plugin.
I could run the code in the view, but I guess that is not the best option.
@mycarousel_itemList = @mycarousel_itemList + "{url: '" + p.photo.url(:thumb) +
"', price: '" + p.price.to_s + " €'},"
Is there an alternative function to change the format of p.price?
To answer your question directly, include the following in your controller (typically near the top, below the class
declaration):
include ActionView::Helpers::NumberHelper
You could also include this module in the model (whatever class p
is), and then write a function to return the formatted price.
The best place for code like this, however, is in a helper, not the controller. The helper would be called from the view. Your controller should be as short as possible and not include any view logic at all.
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