Ruby on Rails:如何正确组织 JS 和 CSS 文件?
我想在我的 Rails 3 应用程序中使用 SlickGrid 插件。 我包含几个应该包含在 HTML 页面中的 JS 和 CSS 文件。
可以将所有需要的 JS 文件放在 public/javascripts
目录中,将所有 CSS 文件放在 public/stylesheets
目录中。但是,我不喜欢这个解决方案,因为它破坏了插件包文件结构。
我想将所有插件文件放在一个地方(我想到了供应商/插件,这是一个更好的地方吗?),并从那里包含所需的文件。这可能吗?
集成插件时包含 JS 和 CSS 文件的正确方法是什么?
I would like to use the SlickGrid plugin in my Rails 3 application.
I contains several JS and CSS files that I should include in my HTML page.
It is possible to put all the needed JS files in the public/javascripts
directory and all the CSS files in the public/stylesheets
directory. However, I don't like this solution because it breaks the plugin package files structure.
I would like to put all the plugin files in one place (I thought about vendor/plugins
, is it a better place?), and include the needed files from there. Is that possible ?
What is the proper way to include the JS and CSS files when integrating a plugin ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 Jammit 可以帮助您完成您想要做的事情。除了打包、嵌入、压缩您的资源之外,它还允许您将 javascript 和样式表存储在应用程序中的任何位置。不过,图像(如果未嵌入)可能是一个问题。
I think Jammit can help you accomplish what you're trying to do. Besides packaging, embedding, gzipping your assets, it also allows you to store javascript and stylesheets anywhere in your app. Images (if not embedded) could be a problem though.
@rubiii 的回答也很好,但是自从 sprockets gem 从版本 2.10.0 支持 bower,现在可以非常轻松地集成任何 js/css 库。此外,版本管理和更新就像输入
bower install
一样简单。 Bower 可以通过 Nodejsnpm install -g Bower
安装,在应用程序的根目录中包含.bowerrc
文件,其中包含内容:然后在
bower.json
中指定库code> 并运行bower install
安装组件后,像往常一样需要
application.js/application.css
中的文件。例如Answer by @rubiii is also good, but since sprockets gem from version 2.10.0 supports bower, now it is extremely easy to integrate any js/css libraries. Also version management and updating as easy as typing
bower install
. Bower can be installed through nodejsnpm install -g bower
, include.bowerrc
file in root of application with content inside:Then specify libraries in
bower.json
and runbower install
After components installed, require files in
application.js/application.css
as usually. e.g.