Rails-ERB忽略了JS/jQuery [英] Rails -- ERB is ignoring JS / jQuery

查看:90
本文介绍了Rails-ERB忽略了JS/jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,以下代码如下:

I have the following code below in my view:

<script type="text/javascript">    
  $(document).ready(function(){    
    $("#button").click(function(event){
      if(false) { //even with if (false), this code is still being executed!
        <% if obj %>
          <% my_method(obj) %>
        <% end %>
        $("#button").attr("href","/" + obj.my_path);
      }
    });        
  });
</script>

从注释中可以看到,JavaScript if语句的条件为false,因此不应执行该块.但是由于某种原因,好像我的JS不存在,正在调用ERB块.为什么会这样呢? JS在该网站的其他页面上似乎运行良好.

As you can see by the comment, the condition for the JavaScript if statement is false, so the block should never be executed. Yet for some reason it is like my JS does not exist and the ERB block is being called. Why is this happening? JS seems to work fine on other pages on the site.

如果您需要更多有关此问题的信息以帮助解决问题,请询问,我将尽力提供尽可能多的信息.

If you need more info about this to help solve the problem ask away and I will try to provide as much information as I can.

推荐答案

对于服务器端代码和客户端代码之间的区别,您似乎有些困惑.

You seem to be a bit confused about the difference between server-side and client-side code.

ERB在服务器上执行,JavaScript在客户端上执行.当您执行ERB时,Ruby几乎会看到以下情况:

The ERB is executed on the server, the JavaScript on the client. When your ERB is being executed, Ruby pretty much sees this:

puts '<script type="text/javascript">'
puts '  $(document).ready(function(){'
puts '    $("#button").click(function(event){'
puts '      if(false) {'
if obj
  my_method(obj)
end
puts '        $("#button").attr("href","/" + obj.my_path);'
puts '      }'
puts '    });'       
puts '  });'
puts '</script>'

再简化一点,ERB可以归结为以下内容:

Simplifying a little bit more, the ERB boils down to something like this:

puts some_strings[0]
puts some_strings[1]
puts some_strings[2]
puts some_strings[3]
if obj
  my_method(obj)
end
puts some_strings[4]
puts some_strings[5]
puts some_strings[6]
puts some_strings[7]
puts some_strings[8]

解开ERB时,很明显为什么要执行my_method(obj).

When you unwrap the ERB, it becomes pretty obvious why my_method(obj) is being executed.

在ERB地区,您所有的JavaScript只是一堆未解释的字符串:您的JavaScript只是一堆毫无意义的文本,直到它到达客户端浏览器为止.

In ERB land, all your JavaScript is just a bunch of uninterpreted strings: your JavaScript is just a bunch of meaningless text until it gets to the client browser.

然后,浏览器将看到以下内容:

Then, the browser will see something like this:

<script type="text/javascript">    
  $(document).ready(function(){    
    $("#button").click(function(event){
      if(false) {
        $("#button").attr("href","/" + obj.my_path);
      }
    });        
  });
</script>

您的ERB无处可见,因为它是在服务器上处理的.但是,可以看到您的if(false),它将由客户端解释.但是,您的ERB已经运行,因此if(false)不能影响您的my_method(obj) Ruby调用.

Your ERB is nowhere to be seen because it was handled on the server. But, your if(false) is seen and it will be interpreted by the client. However, your ERB has already run so the if(false) cannot affect your my_method(obj) Ruby call.

这篇关于Rails-ERB忽略了JS/jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆