在 Rails 3 中使用 jQuery 插件
完成 Hartl 出色的 Rails 教程 后,我现在正在学习 Castledine 和 Sharkie 编写的非常好的 jQuery - Novice to Ninja。虽然我能够在我的 Rails 项目中包含 jquery.js 和 jquery-ui.js,但在添加其他 jQuery 插件时我遇到了困难。
我发现当插件托管在 github 上时,我可以使用命令行将它们导入到我的项目中,并且:
[my rails app]$ rails install [github URL]
插件已成功导入到 /vendor/plugin/ 目录中,其中 javascript_include_tag 找不到它们。这种情况提示了 3 个问题:
有没有办法将 jQuery 插件导入到我的 Rails 应用程序中,将它们放入它们所属的 public/javascripts/ 目录中?
如果 1 的答案是“否”,有没有办法让 javascript_include_tag 在vendor/plugins/中搜索脚本?这似乎是一种不好的做法,因为 jQuery 脚本隐藏在人们通常会查找它们的 public/javascript/ 目录之外。
如果 1 和 2 都是“否”,是否有比 $mv 或复制粘贴更优雅的方式从供应商/插件/移动文件?
注意:通过网络搜索,我在 http://getsprockets.org/ 发现了 37signals 的 sprokets gem,看起来像它可能在某些时候有用,但超出了我当前的需求和理解。
感谢任何想法!
Having finished Hartl's great Rails Tutorial I'm now working my way through the very good jQuery - Novice to Ninja by Castledine and Sharkie. While I've able to include jquery.js and jquery-ui.js in my rails projects, I'm getting stuck when it comes to adding other jQuery plugins.
I've figured out that when the plugins are hosted on github, I can import them into my project using the command line and:
[my rails app]$ rails install [github URL]
The plugin gets successfully imported into the /vendor/plugin/ directory where javascript_include_tag can't find them. This situation prompts 3 questions:
Is there a way to import jQuery plugins into my Rails app that puts them into the public/javascripts/ directory where they belong?
If the answer to 1 is "no," is there a way to have javascript_include_tag search for scripts in vendor/plugins/? This seems like bad practice since jQuery scripts are hidden away from the public/javascript/ directory where people would normally look for them.
If 1 and 2 are both "no's," is there a more elegant way to move the files from vendor/plugins/ than $mv or copying and pasting?
Note: Through web searching I've come across 37signals' sprokets gem at http://getsprockets.org/ which seems like it might be useful at some point but beyond my current needs and understanding.
Grateful for any thoughts!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在当前的 Rails 版本中,目录是
In current rails version the directory would be
通常的做法是下载您想要使用的插件的缩小版本并将其保存在
public/javascripts
目录中。rails install
适用于 Rails 插件,而不是 jQuery 插件。Normal practice is to download a minified version of the plugin you'd like to use and save it in the
public/javascripts
directory.rails install
is for Rails plugins, not jQuery plugins.如果您坚持使用 Rails 安装,您可以编写一个 rake 或 thor 任务来为您复制文件。
您还可以尝试在 public/javascripts 目录和下载目录之间建立符号链接,以便样式表标签可以访问它。
If you are insistent on using rails install you could write a rake or thor task to copy the files for you.
You could also try a symlink between the public/javascripts directory and the download directory so the stylesheet tag can just access it.