Y
Y
Yuri2017-05-04 23:16:32
JavaScript
Yuri, 2017-05-04 23:16:32

Why can't call methods of connected js plugins in RoR?

Good day!
Given:

  • RoR 5.0.2
  • established
    • gem 'clockpicker-rails'
    • gem 'bootstrap-datepicker-rails'

  • application.js:
    • //= require jQuery
    • //= require bootstrap
    • //= require smoothscroll
    • //= require bootstrap/clockpicker
    • //= require bootstrap-datepicker
    • //= require rails.validations
    • //= require rails.validations.simple_form
    • //= require turbolinks
    • //= require_tree .

  • Script in which I make calls:
    ready = ->
      $('.clockpicker').clockpicker
      $('.datepicker-inline').datepicker
    $(document).ready(ready)
    $(document).on('turbolinks:load', ready)

  • Just in case, the whole Gemfile:
    source 'https://rubygems.org'
    
    git_source(:github) do |repo_name|
      repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
      "https://github.com/#{repo_name}.git"
    end
    
    
    # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
    gem 'rails'#, '~> 5.1'
    # Use Puma as the app server
    gem 'puma'
    # Use SCSS for stylesheets
    gem 'sass-rails'
    # Use Uglifier as compressor for JavaScript assets
    gem 'uglifier'
    # Use CoffeeScript for .coffee assets and views
    gem 'coffee-rails'
    # See https://github.com/rails/execjs#readme for more supported runtimes
    # gem 'therubyracer', platforms: :ruby
    
    # Use jquery as the JavaScript library
    gem 'jquery-rails'
    # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
    gem 'turbolinks'
    # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
    gem 'jbuilder'
    # Use Redis adapter to run Action Cable in production
    # gem 'redis', '~> 3.0'
    # Use ActiveModel has_secure_password
    gem 'bcrypt', '~> 3.1.7'
    
    # Use Capistrano for deployment
    # gem 'capistrano-rails', group: :development
    
    #template
    gem 'slim-rails'
    gem 'simple_form'
    gem 'client_side_validations'
    gem 'client_side_validations-simple_form'
    gem 'clockpicker-rails'
    gem 'bootstrap-datepicker-rails'
    gem 'devise-bootstrap-views'
    
    #users
    gem 'devise'#, git: 'https://github.com/gogovan/devise.git', branch: 'rails-5.1'
    gem 'devise-i18n'
    # gem 'high_voltage'
    ## seo
    gem 'meta-tags'
    gem 'friendly_id'
    
    # gem 'mongoid', '~> 6.1.0'
    gem 'flag_shih_tzu'
    
    
    group :development, :test do
      # Call 'byebug' anywhere in the code to stop execution and get a debugger console
      gem 'byebug', platform: :mri
    end
    
    group :development do
      # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
      gem 'web-console'
      gem 'listen'
      # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
      gem 'spring'
      gem 'spring-watcher-listen'
      gem 'better_errors'
      gem 'pg'
    end
    
    # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
    gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
    
    group :production do
      # gem 'mysql2', '~> 0.3.18'
    end


There are no errors in the console. In the browser console when trying to execute code, for example:
$('.datepicker-inline').datepicker();
I get the error
TypeError: $(...).datepicker is not a function
The datepicker gem is here https://github.com/Nerian/bootstrap-datepicker-rails
There are two options in the usage guide. Variant with works for me - it is called for a text field.
I tried to disable turbolinks - removed the gem and removed required from application.js
Placed the code for calling the datepicker() function on an element directly in the view - I got an error in the browser console that datepicker() is not a function.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri, 2017-05-06
@Winter_Silence

The problem was that it was not correctly hanging handlers to call plugin methods. It was necessary like this:

ready = ->
  $('.clockpicker').clockpicker
  $('.datepicker-inline').datepicker
$(document).ready(ready)
$(document).on('turbolinks:load', ready)

B
Bogdan, 2017-05-05
@bogdan_uman

Why don't you use jQuery datepicker?
app/assets/javascripts/application.js:2

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jquery-ui/i18n/datepicker-uk

gemfile
gem 'jquery-rails'
gem 'jquery-ui-rails'

well, the use itself
#  Конечная дата фильтрации
    $( '#date_start' )
      .val GetSession $sessionKey, 'date_start'
      .data 'old-value', GetSession $sessionKey, 'date_start'
      .datepicker onSelect: ->
        $this = $( @ )
        $thisVal =  $this.val( )

        if $thisVal isnt $this.data 'old-value'
          SetSession $sessionKey, 'date_start', $thisVal
          $this.data 'old-value', $thisVal
          $dateEnd = $( '#date_end' )
          $dateEndVal = $dateEnd.val( )

          if not $dateEndVal || moment( $thisVal, $formatDate ).isAfter( moment( $dateEndVal, $formatDate ) )
            SetSession $sessionKey, 'date_end', $thisVal
            $dateEnd.val( $thisVal ).data 'old-value', $thisVal

          filterTable( ) # Фильтрация таблицы документов

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question