简体   繁体   English

仅在某些页面上包含JavaScript或文件的rails

[英]rails including javascript or files only on certain pages

Ok Im new to rails in general and these default loaders and cache loaders an all that stuff they make some sense to me. 好吧,我是Rails的新手,这些默认的加载器和缓存加载器对我来说很有意义。 But my question is. 但是我的问题是。 If I want to include a certain JS file or 2 or specific script on a particular page how would I do that. 如果我想在特定页面上包含某个JS文件或2或特定脚本,该怎么做。

with rails I have app/views/layouts/application.html.erb in this file I gather is the base template for my site/service, and in there I would put all the moving parts. 带有滑轨我在这个文件中收集了app / views / layouts / application.html.erb,这是我的站点/服务的基本模板,在其中放置了所有活动部件。 However I have an occasional need where I only need a javascript file loaded on a particular page. 但是,我偶尔需要只在特定页面上加载javascript文件的地方。 But it needs to be called after the jquery file so jquery is loaded into memory prior to the file I want loaded. 但是需要在jquery文件之后调用它,以便将jquery在要加载的文件之前加载到内存中。 So Im not exactly sure how I would approach that. 所以我不太确定我该如何处理。 Cause in the layout I have the javascript loader line whatever it is exactly I dont remember but its :default none the less which means jquery and applications will load out by default from what the API tells me. 原因是布局中有我不记得的javascript加载器行,但它的:default仍然如此:这意味着jquery和应用程序将默认从API告诉我的条件下加载。

Which does bring me to another question the guy who initially set up the rails server we have added a file to the defaults I would like to mimic that but don't know how with that either. 这确实使我想到了另一个问题,最初设置了rails服务器的那个人我们已经向默认设置中添加了一个文件,我想模仿这个文件,但也不知道该怎么做。

content_for might help you, look at the example that includes the piece of code: <%= yield :script %> content_for可能会帮助您,查看包含以下代码段的示例: <%= yield :script %>

Alternatively, think about ways to allow the JS code to detect if it is begin executed on the correct page (maybe a class or id set on the body tag), and only execute if that condition is met. 或者,考虑允许JS代码检测它是否开始在正确页面上执行的方法(可能是在body标签上设置的类或ID),并且仅在满足该条件时才执行。 Then you can serve your entire javascript collection compressed and minified to the user's browser on the first page load, increasing site performance. 然后,您可以在第一页加载时将整个javascript集合压缩并缩小到用户的浏览器中,从而提高站点性能。

The simplest way would be to just include the script file in the view where you need it. 最简单的方法是将脚本文件仅包含在需要的视图中。 jQuery will have already been loaded in the layout. jQuery将已经加载到布局中。 Alternatively, you can use content_for, as ctcherry mentions. 另外,您可以使用content_for,如ctcherry所述。 You can find a more detailed explanation here: Javascript Include Tag Best Practice in a Rails Application 您可以在此处找到更详细的解释: Javascript在Rails应用程序中包含标记最佳实践

Also, regarding you last question, I'm not sure I understand it correctly, but you can add more options to the javascript_include_tag separated by a comma: 另外,关于您的最后一个问题,我不确定我是否理解正确,但是您可以向javascript_include_tag添加更多选项,并用逗号分隔:

javascript_include_tag :defaults, "my_other_file", "etc"


May be used for this: 可以用于:

<head>
<title>My blog</title>
<%= yield(:head) -%>
</head>

And send it there from a view: 并从视图发送到那里:

<%- content_for(:head) do -%>
<%= javascript_include_tag :defaults -%>
<%- end -%>

It's good work! 很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM