Error Rails Assets. Javascript not working properly (Datepicker)

0

I want to add the bootstrap-datepicker to my project. I did it this way.

Gemfile:

source 'https://rails-assets.org' do
  gem 'font-awesome-sass',                  '~> 4.5.0'
  gem 'rails-assets-bootstrap-sass',        '~> 3.3.6'
  gem 'rails-assets-bootstrap-datepicker',  '~> 1.6.0'
end

_form.html.erb :

<div class="row">
   <div class="col-md-2">
      <%= f.input :publish_at, as: :string, input_html: { class: 'datepicker', 
            value: (Time.now + 1.day).strftime('%d/%m/%Y') } %>
   </div>
</div>

I created a file named datepicker.js within app / assets / javascripts:

$('.datepicker').datepicker({
  language: 'pt-BR',
  format: 'dd/mm/yyyy',
  startDate: '0d',
  autoclose: true,
  keyboardNavigation: false,
  todayHighlight: true
});
alert("hello");

alert is working, though it's as if I'm not picking the '.datepicker' class from my _form.html.erb . When I put the same javascript code inside my _form.html.erb within a tag tag the datepicker works fine, however I did not want to insert the script tag into every view that contains a datepicker. p>     

asked by anonymous 08.03.2016 / 15:18

1 answer

0

I recommend using the gem 'bootstrap-datepicker-rails'.

In app / assets / stylesheets / application.css, add:

*= require bootstrap-datepicker3

And in app / assets / javascripts / application.js

//= require jquery //= require jquery.turbolinks //= require jquery_ujs //= require turbolinks //= require bootstrap-datepicker //= require_tree .

And in your view

<input type="text" data-provide='datepicker' >

If you want to call the function via javascript put it inside:

$(document).ready(function(){ $('.datepicker').datepicker(); });

Also add the gem 'jquery-turbolinks',' ~ > 2.1 'if you use turbolinks

And that's it, it should work. Documentation link

    
08.03.2016 / 19:34