Rails:自动包含操作/控制器特定的 javascript 文件?
我是 Rails 开发的初学者,从 cakephp 搬来的。
在 cakephp 中,您可以在布局中执行一些操作,以自动包含与控制器或操作名称相同的任何 javascript 文件。
例如,在 www.website.com/post/add 中,
如果存在 post.js 和 post/add.js ,它们都会自动加载。
有没有办法在 Rails 中做到这一点?我尝试用谷歌搜索,但不知道要搜索什么,也没有找到太多。
(与CSS相同)
I am a beginner at rails development and moved from cakephp.
In cakephp there's something you can do in the layout to automatically include any javascript files that are named the same as either the controller or the action.
For instance in www.website.com/post/add
both post.js and post/add.js will automatically be loaded if they exist.
Is there a way to do this in rails? I tried doing a google but didn't know what to search for and didn't turn up much.
(Same thing with css)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道 Rails 中有这种功能,
但你可以简单地在布局中执行此操作:
它不会检查文件是否存在,因此你可以进一步创建应用程序帮助程序,然后将 js 逻辑移到那里,包括并检查文件是否存在:
但自 Rails 3.1 以来,有一个资产管道与 application.js 清单文件,所以也许您想了解更多相关信息。
I'm not aware of this kind of functionality in rails,
but you can simply do this in a layout:
it will not check if the file exists, so you can go further and create application helper, and move there a logic of js including and checking if file exists:
But since rails 3.1 there is an asset pipeline with application.js manifest file, so maybe you would like to read more about it.