Mask in Rails fields

Asked

Viewed 1,978 times

2

Guys I’m getting beat up for something theoretically simple, I’m wanting to put a date mask on my application.js:

//= require maskedinput
jQuery(function($){
$("‪#‎datadoacao‬").mask("99/99/9999");
});

In my partial form:

<%= f.text_field :data_doacao, id: 'datadoacao' %>

And I’m using the Gem 'maskedinput-rails'.

In my template I make the following call

Then I imported javascript directly into application.html.erb

<%= javascript_include_tag 'application', 'maskedinput' %>

Does anyone know why it’s not working?

Obs: added the maskedinput file to assests/javascript

  • Any error in console?

  • On your Rails console brings no error message?

4 answers

1

Hello, what you can also do is check if the element is getting the id you entered, what I usually use is the following:

<%= f.text_field :data_doacao, input_html: {'id': 'idQueQuer'} %>

But Inspecione the element and check if it is with the correct id.

1

Then I imported javascript directly into application.html.erb

<%= javascript_include_tag 'application', 'maskedinput' %>

1

How have you done the require in his application.js, make sure you did the require also of JQuery

//= require maskedinput
//= require jquery
jQuery(function($){
   $("‪#‎datadoacao‬").mask("99/99/9999");
});

Your javascript_include_tag already contains it, so enough:

<%= javascript_include_tag 'application' %>

Also check your console for errors, if you’re using Chrome just press F12 and select the tab console.

If everything is OK, something like:

inserir a descrição da imagem aqui

In case there’s a mistake, something like:

inserir a descrição da imagem aqui

I don’t know if it was very basic, but you display some error message can help a lot in diagnosing the problem.

1

Opa, dude, from the look on MEIOMASK, I like it, and I think it respectively simple... I downloaded in this repository (https://github.com/johnvoloski/meiomask-rails), operation is more or less like below:

'z': /[a-z]/,
'Z': /[A-Z]/,
'a': /[a-zA-Z]/,
'*': /[0-9a-zA-Z]/,
'/': /[/]/,
'n': /[0-9]/,
'@': /[0-9a-zA-ZçÇáàãâéèêíìóòôõúùü]/

$('#datadoacao‬').setMask({mask: "nn/nn/nnnn"});

I hope it helps

Browser other questions tagged

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