简体   繁体   English

Rails 4中的页面特定Javascript代码

[英]Page Specific Javascript code in rails 4

I have seen many answers to similar posts on Stackoverflow and I tried implementing it but it is not working. 我在Stackoverflow上看到了许多类似帖子的答案,我尝试实现它,但是没有用。 I have done the following things 我做了以下事情

1) Removed //= require_tree . 1)删除// = require_tree。 from assets/javascripts/application.js 来自资产/javascripts/application.js

2) Added //= require users in the assets/javascripts/application.js (where users.js is my javascript file in the same folder as the application.js resides) 2)在资产/javascripts/application.js中添加了// =要求用户(其中​​users.js是我的javascript文件,与application.js驻留在同一文件夹中)

In my new.html.erb I have included the following line 在我的new.html.erb中,包含了以下行

   <%= javascript_include_tag "users" %>

3) users.js contains simple javascript code like "alert(6)" 3)users.js包含简单的javascript代码,例如“ alert(6)”

When I make request to users/new thru localhost I do not get any alert although I can see my users.js included in the html but there is no code in that. 当我通过本地主机向users / new发出请求时,虽然可以看到html中包含的users.js,但没有任何提示,但是没有任何代码。 Can anyone please help me on this?? 有人可以帮我吗?

Since you have included users.js inside application.js , you should add application.js to the page 由于您已将users.js包含在application.js ,因此应将application.js添加到页面中

In new.html.erb , replace users with application new.html.erb中 ,将users替换为application

Remove this line 删除此行

<%= javascript_include_tag "users" %> 

And add this line 并添加此行

<%= javascript_include_tag "application" %>

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

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