1

I have a basic .js file with this inside it

//$('#show').html('<%= escape_javascript(render(:partial => 'show')) %>');

When the .js file is called, the code above gets executed and the partial is rendered even though it's commented out. When the code is deleted, the partial never gets rendered. The DOM remains unchanged, but I can see the partial being rendered by the server at the command line. What gives?

Steve
  • 4,446
  • 7
  • 35
  • 50
  • 2
    we can't tell from this angle. have you cleared the cache? – Joseph May 19 '12 at 09:40
  • I would suggest to put a breackpoint on that line and look at the call stack. – andreapier May 19 '12 at 09:43
  • @Joseph I cleared the cache and still the same behavior – Steve May 19 '12 at 09:53
  • Is it a "basic JS file" or is it an erb template? – Dave Newton May 19 '12 at 11:31
  • When you say the partial is rendered are you saying the JavaScript ends up displaying that data to the user or are you only talking about rails rendering the template. – Frederick Cheung May 19 '12 at 15:35
  • @FrederickCheung the controller runs as JS and makes a call to the .js file in question, when the code is there (with or without the double slash) rails will try to render the partial. When the code is deleted completely the partial isn't rendered – Steve May 19 '12 at 22:31

4 Answers4

4

You're commenting out the JavaScript--which executes on the client side.

The partial rendering happens on the server side, before the client ever sees the rendered JavaScript.

In other words, commenting out JavaScript has zero effect on the server-side processing. If you don't want the server-side string to be displayed, comment it out:

<%#= escape_javascript(etc) %>

Let's assume the partial renders like this:

<h1>Foo bar baz</h1>
<div>Plugh!</div>

Escaping this for JavaScript will turn the newline into a \n (and would escape single- and double-quotes, etc.) leaving you with this on the client side:

$('#show').html('<h1>Foo bar baz</h1>\n<div>Plugh!</div>');

Whether or not the JS is commented out or not, the partial is going to render unless you comment out the results of the escape_javascript Ruby code.

On the client side, if the JS is commented out, it shouldn't update show's HTML--are you saying it is?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • But the call to the partial happens in the JavaScript so if that's commented out shouldn't the call never go through? – Steve May 20 '12 at 05:21
  • Nope. Your erb template produces a lump of javascript. It has no knowledge of the meaning of that lump. – Frederick Cheung May 20 '12 at 09:01
  • @Steve There's no "in" the JavaScript--that's what I'm saying, the two types of code are executed in two different places. Client-side JavaScript executes on the client side, Ruby executes on the server. – Dave Newton May 20 '12 at 10:04
  • When the DOM is initialized the show partial hasn't been rendered yet. The call to the .js file is made and the JavaScript code replaces what's inside the #show div with the partial 'show'. From what I understand, this is when the call to render the partial is sent to the server. If this JavaScript code is deleted the message to the server never goes through. If the JavaScript is commented out, isn't that almost the equivalent of deleting the code (in terms of what the interpreter sees). Thanks for the help Dave – Steve May 20 '12 at 20:34
  • @Steve No! The JS file, by the time the client sees it, has the partial rendered as a string, inside the `$('#show').html('<<>>');` code. If you comment out the JS, the partial is still rendered, because the render happens before the client sees the file. See updated answer; are you saying the HTML of `#show` is being updated if the JS is commented out? – Dave Newton May 20 '12 at 20:42
  • No it's not updating show's HTML. I understand now thank you very much Dave! – Steve May 20 '12 at 21:08
0

Check to see if this is the line or you have this call some place else:

//$('#show').html('<%=escape_javascript(render(:partial => 'show'))%>');alert('This is the line');
Marian Zburlea
  • 9,177
  • 4
  • 31
  • 37
  • It is the line because once I delete it the call to the partial no longer gets executed – Steve May 19 '12 at 09:53
0

Perhaps your escape_javascript function is inserting a newline? That would end the comment and cause anything after the newline to be executed.

Brilliand
  • 13,404
  • 6
  • 46
  • 58
  • I removed it and still the same behavior – Steve May 19 '12 at 09:54
  • Try saving the .js file from within your browser. That should eliminate ruby on rails from the equation - you can then check properly whether any newlines are being inserted into the comment. – Brilliand May 19 '12 at 10:34
  • `escape_javascript` is a Rails helper; AFAIK it doesn't insert random newlines. – Dave Newton May 19 '12 at 11:30
0

I had a similar issue and figured out a work around for what I wanted it for. Perhaps something like this will help you out!

Instead of commenting the lines of code out, try just building a logical operator that gives you an "on/off" toggle on the functions you want to run. I set mine up like this so I could turn off the script but keep it for future reference:

var toggle = 1; 

if(toggle == 2){
   functions that you want to run when turned on... 
}

Since your variable is not equal to two, the if statement will not render out what's stored inside of it, effectively 'commenting' that code out for you! To turn it back on all you have to do is change the 'toggle' variable back to 2 (or whatever 'on' value you want). Just a little work around I found for myself, hope that helps!

Jon Hendershot
  • 456
  • 2
  • 6
  • 15
  • At the time I asked this question, I had trouble understanding why the embedded Ruby code cited in the question was being executed even though I thought it was commented out. Thanks to the responses, I now know that the embedded Ruby is run before the code is treated as javascript. I think you misunderstood my question a bit, and on a second look my question did lack details. I have edited the question to add clarity. Thanks for your answer. – Steve Dec 02 '14 at 22:38