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/
over 1 year ago
·
Works like a charm! Thanks!
over 1 year ago
·
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
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Filed Under
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#