简体   繁体   English

不会以.js.erb扩展名加载Javascript

[英]Javascript won't load with .js.erb extension

I have a javascript file in my rails site which loads fine with the extension .js. 我在Rails网站上有一个javascript文件,该文件可以很好地使用扩展名.js加载。

I need to use an image helper tag in it, so according to the documentation I need to add an erb extension to - making it .js.erb. 我需要在其中使用一个图像帮助器标记,因此根据文档,我需要添加一个erb扩展名-使它成为.js.erb。

As soon as I change the extension to .js.erb (leaving the javascript file as is, IE no ruby) it breaks and the javascript no longer works. 一旦我将扩展名更改为.js.erb(将javascript文件保持原样,即IE没有ruby),它就会中断,并且javascript不再起作用。

When I check it with chrome inspector the javascript contents are as follows: 当我用chrome inspector检查它时,javascript内容如下:

(function() {


}).call(this);

However it is fine when the extension is just .js. 但是,当扩展名仅是.js时很好。

What am I doing wrong? 我究竟做错了什么?

edit:: without the .erb extension the relevent javascript line is returning this in chrome inspector: 编辑::没有.erb扩展名,relevent javascript行在chrome inspector中返回此代码:

function fancyViewReport() {
    return '<div class="demo-report-title"><a class="fancyReport" href="<%= asset_path 'demoreport.png' %>">Click <u>here</u> to view a demo report!</a></div>';
}

this javascript file is being called through application.js 这个JavaScript文件正在通过application.js调用

As far as I know, js.erb are made for ajax type requests. 据我所知, js.erb是为ajax类型请求而创建的。 I think that if your ajax_method called is under controller someController and if you have someController.js.erb file, you should put it in app/views/someController and make sure you have somethig like that 我认为,如果您调用的ajax_method在controller someController并且如果您具有someController.js.erb文件,则应将其放在app/views/someController ,并确保您具有诸如此类的somethig

def ajax_method
    ...
    respond_to do |format|
      format.js
    end
  end

好的,我最终通过创建一个新文件并复制粘贴了以前的javascript文件的内容来解决了这个问题,这很奇怪。

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

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