本文介绍了Ruby on Rails资产管道无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才一直在从事红宝石on Rails项目.我创建了一个名为"animals"的控制器和一个用于索引操作的视图(index.html.erb).我不想包含应用程序" javascript文件.

I have been working on a ruby on rails project just now. I have created a controller named 'animals' and a view (index.html.erb) for index action. I don't want to include 'application' javascript file.

所以我创建了animals.js

So I created animals.js

文件内容为

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= animals.coffee

我还已经将animal.js和animals.css添加到asset.rb

I have also added animals.js and animals.css to asset.rb

Rails.application.config.assets.precompile += %w( animals.js )
Rails.application.config.assets.precompile += %w( animals.css )

我的index.html.erb包含以下几行

My index.html.erb contains following lines

<%= stylesheet_link_tag 'animals', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'animals', 'data-turbolinks-track' => true %>

当我检查服务器生成的源html代码时,我发现页面中未包含jquery scipts.这只是animal.js文件.

When I inspected source html code generated by server, I have seen that jquery scipts hadn't been included in the page. It was just animal.js file.

<head>
   <meta name="viewport" content="width=device-width, initial-  scale=1.0">
   <title>Rails Omniauth</title>
   <meta name="description" content="Rails Omniauth">
   <link rel="stylesheet" media="all" href="/assets/animals.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b5433a495991b7852b855.css?body=1" data-turbolinks-track="true">
   <script src="/assets/animals.self-1d93d37bf2f830ac42e82eb68e3bb0040ece5d23533a05bf77f7407fd59178d3.js?body=1" data-turbolinks-track="true"></script>

</head>

任何建议为何包含这些脚本?

Any suggestions why these scripts had been inclued ?

谢谢.

推荐答案

我认为您的问题可能是您有两个具有相同名称(但扩展名不同)的文件.由于Rails资产助手会平等对待coffescript和js文件,因此这是模棱两可的:

I think your issue might be that you have two files with the same name (but different extensions). Since the Rails asset helpers treat coffescript and js files equally this is ambiguous:

<%= javascript_include_tag 'animals', 'data-turbolinks-track' => true %>

应该加载哪个文件?我猜它需要按字母顺序排列的第一个字母,即animals.coffee.

Which file should it load? I'm guessing it takes the first in alphabetical order which would be animals.coffee.

要解决此问题,请将链轮清单移动到animals.coffee并删除animals.js文件.

To solve the issue move the sprockets manifest to animals.coffee and remove the animals.js file.

# app/assets/animals.coffee
#= require jquery
#= require jquery_ujs
#= require turbolinks
#= require bootstrap-sprockets
#= require_self
alert 'Hello World'

这篇关于Ruby on Rails资产管道无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:45