【问题标题】:Jquery/Datepicker works locally but not in production (heroku)Jquery/Datepicker 在本地工作,但不在生产环境中(heroku)
【发布时间】:2014-04-27 22:45:11
【问题描述】:

我一直在浏览器检查器上看到的 javascript 错误消息说

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

它在本地工作得很好。根据我想象的错误,它只是无法识别 jQuery 语法。不知道如何修复它。非常感谢您的帮助!

我的application.js如下:

//= 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'});
 });

我的 gem 文件有 gem:

'jquery-rails', '~> 2.3.0'

我的布局在头部有以下内容:

<%= javascript_include_tag "application" %>

相关表单代码如下:

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

【问题讨论】:

    标签: jquery ruby-on-rails ruby jquery-ui datepicker


    【解决方案1】:

    按照文件本身的这一行所示,将代码放入 application.js 可能不是最好的选择。

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

    在你的视野中试试这个

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

    然后从另一个文件中的 coffescript 像这样调用(这将允许您通过将类 datepicker 添加到应用程序中任何位置的输入元素来构建任意数量的日期选择器)

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

    您也没有提及您的 Gemfile 中是否有 jquery-ui-rails,但如果您没有,这也可能是问题的一部分。

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 1970-01-01
      • 2016-07-16
      • 2018-03-23
      • 2015-05-15
      • 2019-08-17
      • 1970-01-01
      • 2021-03-17
      • 2012-02-16
      相关资源
      最近更新 更多