简体   繁体   中英

Jquery/Datepicker works locally but not in production (heroku)

The javascript error message I keep seeing on the browser inspector says

"ReferenceError: Can't find variable $" on application-ccd035fs...... .js:1"

It works just fine locally though. Based on the error I'm imagining it just isn't recognizing jQuery syntax. Not sure how to fix it. Help would be much appreciated!

My application.js is as follows:

//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree .

$(document).ready(function(){
    $('#showdate').datepicker({date_format: "dd/mm/yyyy", altField: '#mile_date', altFormat: 'yy-mm-dd'});
 });

My gem file has gem:

'jquery-rails', '~> 2.3.0'

My layout has the following in the head:

<%= javascript_include_tag "application" %>

The relevant form code is as follows:

<div class="field">
    <%= f.label :date, "Date:" %><br />
    <%= f.text_field :date, :id => "showdate" %>
 </div>
    <%= f.hidden_field :date, :id => "mile_date" %>

Probably not best to put code into application.js as is indicated by this line in the file itself.

// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.

try this in your view

<div class="field">
    <%= f.label :date, "Date:" %><br />
    <%= f.text_field :date, :class => "datepicker" %>
    <%= f.hidden_field :date %>
</div>

then call like this from coffescript in another file (this will allow you to build as many datepickers as you want by just adding the class datepicker to an input element anywhere in your application)

$ ->
  $("input.datepicker").each (i) ->
    $(this).datepicker
    altFormat: "yy-mm-dd"
    dateFormat: "dd/mm/yy"
    altField: $(this).next()

Also you did not mention if you have jquery-ui-rails in your Gemfile but if you don't this may also be part of the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM