【问题标题】:Rails 5 assets pipeline not working as expected (Development)Rails 5 资产管道未按预期工作(开发)
【发布时间】:2018-03-29 22:54:39
【问题描述】:

我正在添加我所有的 .js.css 文件以像这样进行预编译

Rails.application.config.assets.precompile =  ['*.js', 'application.scss', '*.scss', '*.css.erb']

并在这样的布局head部分中包含application.js

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

即使某些, js 文件在我看来不可用。我在那个目录中有一个名为image-picker.js 的文件。这使得.imagepicker() 函数在我的视图中可用,但它会抛出这样的错误

Uncaught TypeError: $(...).imagepicker is not a function

但是,当我专门使用
&lt;%= javascript_include_tag "image-picker" %&gt;

为页面添加 .js 文件时

它按预期工作。为什么会这样。我需要在配置中设置其他任何东西吗?如何避免在每个页面视图中包含我的资产??

更新

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require image-picker.js #tried but not working.
//= require_tree .

更新2

我正在使用 jquery-rails - 4.3.1jquery-ui-rails 6.0.1jquery-2.1.4.min.js 添加到我的资产中。但是当我使用 //= require jquery 时,我认为它不包括 jquery 到应用程序。

当我尝试手动添加时,一切正常,

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jquery-2.1.4.min #now everything working fine. it seems a dependency problem.
//= require image-picker.js #tried but not working.
//= require_tree .

我想知道这是什么原因。谢谢。

您可以找到非常相似的问题 - here

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 asset-pipeline


    【解决方案1】:

    尝试在application.js中添加image-picker.js

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

    【讨论】:

    • 如果require_tree . 不存在,请在您的application.js 末尾添加。
    • 它在那里。 @PeterTretyakov
    • 更新了请看@puneet18
    • @Nani require image-picker.js in application.js
    • @puneet18 我在发布问题之前尝试过并且也重新启动了服务器。它不工作。还有一件事,当我们包含required_tree 时,它会将所有.js 文件放在同一个目录中,对吗?再添加有什么用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    相关资源
    最近更新 更多