需要资产路径子目录中的资产文件
我正在尝试将我的应用程序升级到 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 技术交流群。

发布评论
评论(2)
不知道为什么 Rails 只添加一级子目录,但您肯定可以在 config/application.rb 中向管道添加额外的(完全限定的)路径。文档给出了答案: http://guides.rubyonrails.org/asset_pipeline.html#资产组织
例如:
config.assets.paths << “#{Rails.root}/app/assets/flash”
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
看起来rails只向资产路径添加一级子目录
您可以通过在rails控制台运行
Rails.application.config.assets.paths
来检查当前路径,在application.rb中添加文件夹的绝对路径,例如
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 consoleadd absolute path of your folders at application.rb like