Rails create a helper to add text -


i have fee column in model , integer type, try create tiny helper add dollar sign neatly in front. means, instead of writing:

span = "$#{@object.fee}" 

i can write like

span = @object.fee.dollar 

so created tiny helper.

module applicationhelper     def self.dollar         "$#{self.try(:to_s)}"     end end 

i not sure put it, it's showing

undefined method `dollar' 180:fixnum

number_to_currency()

rails 4.2 has actionview::helper

number_to_currency(1234567890.506) 

helper

if want implement helper, works

module applicationhelper     def dollar(amount)        amount = number_to_currency(amount)     end end 

invoke

<%= dollar(your_var_here) %>     

rails spec number_to_currency()

http://api.rubyonrails.org/classes/actionview/helpers/numberhelper.html#method-i-number_to_currency


note: other versions of rails may have function, you'd have check version.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -