简体   繁体   English

自动包含用于布局文件的javascript

[英]Automatically include javascript for layout files

I've got just two views on my very simple application: 在非常简单的应用程序中,我只有两个观点:

app/views/layouts/alpha.html.erb
app/views/layouts/beta.html.erb

I have 'corresponding' coffeescript files for them: 我为他们提供了“相应的” coffeescript文件:

app/assets/javascripts/alpha.js.coffee #-> console.log 'hi, im from alpha'
app/assets/javascripts/beta.js.coffee  #-> console.log 'hi, im from beta'

However, upon visting I get no messages in my console and visiting beta I get no messages in my console. 但是,访问后,我在控制台中未收到任何消息,而访问Beta时,控制台中也未收到任何消息。 How do I automatically load these files? 如何自动加载这些文件?

Just add the following lines: 只需添加以下几行:

For the alpha.html.erb: 对于alpha.html.erb:

Add at the bottom of your layout: 在布局的底部添加:

<%= javascript_include_tag "alpha" %>

And the same for the beta.html.erb beta.html.erb相同

<%= javascript_include_tag "beta" %>

That should do the trick. 这应该够了吧。

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

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