Simple Form different input for a model

Asked

Viewed 228 times

0

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

70.567,54

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

I tried to use the gsub withdraw the "." and convert the "," to dot and let it turn to convert to big decimal, but the Rails is not letting move the hash, I tried to use the gem Money, but it didn’t seem very useful (Or I didn’t quite understand how to use it, it just makes a mistake saying that the value is not a number, of course it comes string and I’m not able to handle it). Maybe I should change the field type in the simple form? But then I wouldn’t show up to Mask.

Does anyone know how I could display Mask and still save the value as and which the user typed in the field (I even thought of using it as a string, but it would take a lot of work later to make calculations of records, sum, average, median etc).

1 answer

0

Well after some tightening I did as follows (May not be the best but it worked):

On my controller

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

This way before saving I get the string that came from the form with the mask and everything and remove the points and commas and adjust to how Rails will be able to read and convert. Then I show in the views the result as follows:

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

Now it’s working like I wanted it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.