需要资产路径子目录中的资产文件

发布于 11-30 20:34 字数 674 浏览 5 评论 0原文

我正在尝试将我的应用程序升级到 Rails 3.1,并且我有很多自定义 Javascript 文件,我正在尝试将它们迁移到新的资产管道。

我当前(即 3.1 之前)的目录结构类似于:

public/
    foo/
        model.js
        controller.js
        etc...

因此,我尝试将它们移动到 lib/assets/javascripts 并保持 foo/ 子目录完整(而不是制作 foo_model.js、foo_controller.js, ETC)。

在我的 application.js 中,我尝试这样做:

//= require foo/model

但是

//= require 'foo/model'

这些不起作用。 Rails 总是给我一个错误页面,并说:

couldn't find file 'foo/model.js'

我确实测试过,如果它们不在子目录中,它们会被正确包含,所以我知道这不仅仅是我的 lib/assets 目录不在包含路径或其他东西中。所以我想我只是想知道是否有办法做到这一点,或者我应该扁平化目录并使用 foo_model.js 等?

I'm in the process of trying to upgrade my app to Rails 3.1, and I have quite a lot of custom Javascript files that I'm trying to migrate to the new assets pipeline.

My current (that is, pre-3.1) directory structure has something like:

public/
    foo/
        model.js
        controller.js
        etc...

So, I'm trying to move these to lib/assets/javascripts and keep the foo/ subdirectory intact (rather than make foo_model.js, foo_controller.js, etc).

In my application.js I tried to do:

//= require foo/model

and

//= require 'foo/model'

But these don't work. Rails always gives me an error page and says:

couldn't find file 'foo/model.js'

I did test that things get correctly included if they're not in the subdirectory, so I know it's not just that my lib/assets directory is not in the include path or something. So I guess I'm just wondering if there's a way to do this, or should I just be flattening the directories and using foo_model.js and such?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满栀2024-12-07 20:34:11

看起来rails只向资产路径添加一级子目录

您可以通过在rails控制台运行Rails.application.config.assets.paths来检查当前路径,

在application.rb中添加文件夹的绝对路径,例如

config.assets.paths += ["/path/to/lib/assets/javascript/foo"]

Looks like rails only add one level of subdirectories to assets paths

You can check with current paths by running Rails.application.config.assets.paths at rails console

add absolute path of your folders at application.rb like

config.assets.paths += ["/path/to/lib/assets/javascript/foo"]
恏ㄋ傷疤忘ㄋ疼2024-12-07 20:34:11

不知道为什么 Rails 只添加一级子目录,但您肯定可以在 config/application.rb 中向管道添加额外的(完全限定的)路径。文档给出了答案: http://guides.rubyonrails.org/asset_pipeline.html#资产组织

例如:

config.assets.paths << “#{Rails.root}/app/assets/flash”

Not sure why rails only adds one level of subdirectories, but you can surely add additional (fully qualified) paths to the pipeline in config/application.rb. The docs lead to the answer: http://guides.rubyonrails.org/asset_pipeline.html#asset-organization

For example:

config.assets.paths << "#{Rails.root}/app/assets/flash"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文