Rails 5资产管道未按预期工作(开发)

时间:2017-10-18 06:13:09

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

我正在将所有.js.css文件添加到预编译中,如下所示

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

并在application.js部分中加入head,如下所示

<%= 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

但是当我特意使用
包含该页面的.js文件时 <%= javascript_include_tag "image-picker" %>

它正如预期的那样工作。为什么会这样。我是否需要在配置中设置其他内容?如何避免在每个页面视图中包含我的资源?

更新

// 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 .

UPDATE2

正在使用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

1 个答案:

答案 0 :(得分:0)

尝试在image-picker.js

中添加application.js
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require image-picker
//= require_tree .