Search code examples
ruby-on-railstwitter-bootstrapsimple-formdata-masking

Passing data-mask bootstrap variable into simple_form input?


I'm customizing a simple_form gem-enabled form for which I'd like the phone number field to have a datamask from http://jasny.github.com/bootstrap/javascript.html#inputmask

My original code is:

  <%= f.input :phone, :required => true, 
            :input_html => { :maxlength => 14} %>

What is the code required to pass something like into the f.input?

Using something like input_html and label_html doesn't seem to work.


Solution

  • in your form try this:

    <%= f.input :phone, :required => true, 
                :input_html => { :maxlength => 14, :"data-mask" => "999-999-9999" } %>
    

    Also, assuming you're running Rails 3.1+ and you have the jasny files in your vendor/assets/javascripts directory, be sure to include the bootstrap-inputmask file in app/assets/javascripts/application.js:

    //= require jquery
    //= require jquery_ujs
    //= require bootstrap
    //= require bootstrap-inputmask
    //= require_tree .