Search code examples
ruby-on-railssimple-formmasking

How can I use masks on f.input on Rails?


First of all, sorry if I made language mistakes, this is not my mother tongue.

I'm learning RoR and I'm building a CRUD to register the information of a lot of computers, and I'm using Simple Form to generate the forms. My question is, how can I use masks in the f.input element?

Like when the user goes to insert the Ip adress, he get something like ---.---.---.--- in the text field.

if helps, here is this part of the form:

<div class="field">
    <%= f.input :mac_adress, label: 'Type the machine\'s MAC ADRESS: ', placeholder: 'XX:XX:XX:XX:XX:XX' %>
</div>
<div class="field"> 
    <%= f.input :patrimony, label: 'What is the patrimony number? ', placeholder: 'XXXXXX' %>
</div>
<div class="field"> 
    <%= f.input :ip, label: 'Inform the IP adress: ', placeholder: 'XXX.XXX.XXX.XXX' %>
</div>
<div class="field"> 
    <%= f.label :lab_id %>: 
    <%= f.text_field :lab_id, :value => @machine.lab.name, :hidden => true %>
</div>
<div class="actions">
    <%= f.button :submit %>
</div>

Thanks for any help, and if any additional information needed just ask.


Solution

  • Mine:

    <%= f.text_field :phone, class: "form-control", :required => true, :data => {:mask=> '999-999-9999'} %>
    

    Yours:

    <%= f.input :ip, label: 'Inform the IP adress: ', placeholder: 'XXX.XXX.XXX.XXX', :data=> {:mask=> '999.999.999.9999' %>
    

    add this js in your application.js

    jasny/jasny-bootstrap.min.js