Brazillian currency fields with simple_form on Ruby on Rails
Do you need to apply masks on currency fields for Brazilian format? Don't worry, I'll show you a little trick. But, first take a look on a previously post that I wrote about delocalization using I18n::Alchemy, because you'll need this gem
Now that you have the gem and are delocalizing attributes, you need to download the minified version of a javascript with multiple masks inserted already.
http://www.meiocodigo.com/projects/meiomask/
Now, call this on your application.js (or somewhere else, maybe a own folder javascript lib):
$(document).ready(function($) {
$("input[type=text].currency").setMask('decimal');
});
The next step is create your own currencyinput for simpleform.
class CurrencyInput < SimpleForm::Inputs::Base
def input
input_html_options[:type] ||= "text"
@builder.text_field(attribute_name, input_html_options)
end
end
Now, you just need to call on your simpleformfor this:
= f.input :value, as: :currency
Done this, your application will automatically mask this field to this 0,00 and reach at maximum this value 100.000.000.000,00
Written by Patrick Müller
Related protips
3 Responses
The project's URL has changed. Try update it directly to the Github's URL:
https://github.com/fabiomcosta/jquery-meiomask/
Works like a charm! Thanks!
What is your way to save this value (with BRL currency) in model?
Why if I submit with value 1,000,000.00, model save with 1
Have a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside