Simple Form different input for a model

0

I have in my database a column value with precision 7 and 2 type big decimal. In my form I use the simple form and I also use the Mask, ie field for value entry would look like this:

70,567.54

However, at the time of saving, the controller receives this as a string and automatically converts, and everything after the first point is lost, getting only 70.0.

I tried to use gsub to remove the "." and convert the "," to point and let it turn into converting to big decimal, but the rails is not letting the hash, I tried to use gem Money , but it did not seem very useful (or I did not quite understand how use it, it only gives error saying that the value is not a number, of course comes string and I'm not able to treat). Maybe I should change the field type in the simple form? But then the mask would not appear.

Does anyone know how I could display the mask and still save the value as the user typed it in the field (I even thought about using it as a string, but it would take a lot of work later to do record calculations, sum, ).

    
asked by anonymous 18.06.2017 / 06:45

1 answer

0

Well after some tightening I did the following (It may not be the best, but it worked):

On my controller

@financial = Financial.new(financial_params) @financial.value = financial_params[:value].gsub(/[.]/, '').gsub(/[,]/, '.')

In this way before saving I get the string that came from the form with the mascara and everything and I remove the dots and commas and I'll adjust how rails can read and convert. Then I show the results in the following way:

<td><%= number_to_currency(financial.value, unit: "R$", separator: ",", delimiter: ".") %></td>

Now it's working the way I wanted it to.

    
18.06.2017 / 18:13