简体   繁体   中英

Javascript file for only some views in Rails 3.1

在Rails 3.1中,什么是仅包含某些视图的javascript文件并将其从其他视图中排除的一种巧妙方法?

In the view you want the script,

<%- content_for :javascript do -%> 
  <script type="text/javascript"> ... </script>
<%- end -%>

An alternative to the above, I use a javascript_include_tag pointing to the js file for that script instead of using the tag so that I can keep all my javascript in one place.

<%- content_for :javascript do -%> 
  <%= javascript_include_tag "example.js" %>
<%- end -%>

In the layout for that view where you have your other javascript:

   <%= yield :javascript %>

在您的特定html.erb文件中添加以下行

<%= javascript_include_tag "name_of_js_file" %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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