Rails 3.1:无法从vendor/assets目录正确提供javascripts吗? [英] Rails 3.1: javascripts not served correctly from vendor/assets directory?

查看:87
本文介绍了Rails 3.1:无法从vendor/assets目录正确提供javascripts吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将javascript文件整理在几个目录中,并且发现了以下奇怪的行为.给出以下树:

+ app
  + assets
    + javascripts
      + common
      + public
        + common
        + home
          - home.js

home.js看起来像这样:

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_directory ../../jquery_plugins
//= require_directory ../../common
//= require_directory ../common
//= require_self

现在,技巧就在jquery_plugins目录中.我将其放置在vendor/assets/javascripts(检查Rails.application.config.assets.paths时包含在资产加载路径中)内.当我这样做时,我得到错误:require_tree argument must be a directory.当我将该目录移到app/assets/javascripts时,一切正常.

有人知道我在做什么错吗?还是这是一个错误?

解决方案

您可以使用以下方法将清单文件添加到要提供服务的目录中 vendor/assets/javascripts/jquery_plugins/manifest.js

//= require_directory .

,并通过

在您的app/assets/javascripts/application.js中要求它

//= require jquery_plugins/manifest

编辑(甚至更简单的方式)

感谢@LeEnno

实际上,您可以将所有与单个库相关的文件放在以库命名的文件夹中,例如vendor/assets/javascripts/bootstrap,然后在同一文件夹中添加index.js作为清单,Rails会自动将其提取

如果在您的

app/assets/javascripts/application.js

您添加行

//= require bootstrap

太容易了!!!
链接到 Rails资产管道指南

I have organized my javascript files in a couple of directories and I have found the following strange behavior. Given the following tree:

+ app
  + assets
    + javascripts
      + common
      + public
        + common
        + home
          - home.js

home.js looks like this:

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_directory ../../jquery_plugins
//= require_directory ../../common
//= require_directory ../common
//= require_self

Now the trick lies in the jquery_plugins directory. I placed this inside vendor/assets/javascripts (which is included in the asset load path, when I check Rails.application.config.assets.paths). When I do this I get the error: require_tree argument must be a directory. When I move that directory to app/assets/javascripts then everything works.

Does anybody have a clue as to what I'm doing wrong? Or is this a bug?

解决方案

You could add a manifest file to the directory you are trying to serve with something like vendor/assets/javascripts/jquery_plugins/manifest.js

//= require_directory .

and require it in your app/assets/javascripts/application.js via

//= require jquery_plugins/manifest

Edit (even simpler way)

Thanks to @LeEnno for this

You can actually put all your single library related files in a folder named after the library for example vendor/assets/javascripts/bootstrap and in that same folder add an index.js which will act as your manifest and Rails will automatically pick it up

if in your

app/assets/javascripts/application.js

you add the line

//= require bootstrap

SO EASY!!!
Link to Rails Asset Pipeline Guide

这篇关于Rails 3.1:无法从vendor/assets目录正确提供javascripts吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆