# JavaScriptGenerator Dead End

**URL:** <https://www.ruby-forum.com/t/javascriptgenerator-dead-end/58273>\
**Category:** Rails\
**Created:** [April 20, 2006, 6:14am UTC](https://www.ruby-forum.com/t/javascriptgenerator-dead-end/58273 "2006-04-20T06:14:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [April 20, 2006, 6:14am UTC](https://www.ruby-forum.com/t/javascriptgenerator-dead-end/58273/1 "2006-04-20T06:14:53Z")

</div>

Hi All,

I posted earlier about a problem I was having with the various remote  
view methods escaping all the javascript in my callbacks. I was quickly  
informed that that’s just what they do, and offered some workarounds.

However, in actually working with this, I’m finding that this one little  
thing is forcing me to write some REALLY ugly code. Where I thought I  
could specify an updater with some fairly sophisticated view  
post-processing in my view, I am forced to write procedural code in my  
controllers.

If I wanted to write code that ugly, I would have stuck with PHP.

There’s got to be a way to stop e.g. link\_to\_remote from escaping the  
javascript specified in the callbacks. This behavior dramtically  
reduces the elegance of these otherwise beautiful methods.

I’ve done some digging through the docs and even some of the code, but  
nothing really jumped out at me. Can someone more familiar with these  
helpers please give me some suggestions?

Thanks much,

-Mike.

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [April 20, 2006, 8:05am UTC](https://www.ruby-forum.com/t/javascriptgenerator-dead-end/58273/2 "2006-04-20T08:05:29Z")

</div>

Can you post your code that you’re having a problem with? why can’t you  
have  
your callbacks call javascript methods and in the methods do all the  
complex  
javascript stuff instead of putting all the complex javascript stuff in  
the  
callback parameter?

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [April 20, 2006, 8:25am UTC](https://www.ruby-forum.com/t/javascriptgenerator-dead-end/58273/3 "2006-04-20T08:25:48Z")

</div>

OK, here’s the simplest example of why what I’m asking for is useful:

\<% @foo=‘bar’\>  
\<%= link\_to\_remote :url=\>{:parial =\> ‘foo’},  
:update =\> ‘bar’,  
:oncomplete =\> “new Effect.Highlight(’#{@foo}’)”  
%\>

What I’m trying to do is send the contents of ruby variables to the  
javascript code as literals. It seems like a perfectly natural thing to  
do, but the quote escaping makes it impossible, yielding:

new Effect.Highlight(&squot;bar&squote;)

This works surprisingly frequently considering what it looks like, but  
is unreliable, and just plain wrong.

> Can you post your code that you’re having a problem with? why can’t you  
> have  
> your callbacks call javascript methods and in the methods do all the  
> complex  
> javascript stuff instead of putting all the complex javascript stuff in  
> the  
> callback parameter?

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [April 20, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/javascriptgenerator-dead-end/58273/4 "2006-04-20T22:45:51Z")

</div>

OK, I couldn’t wait for you guys, so I found the problem myself :).  
What’s happening is that the tag\_options method in tag\_helper.rb runs  
all tag options through html\_escape.

I think of this as a bug which can be fixed by simply taking out the  
html\_escape call in tag\_options, so it looks like this:

def tag\_options(options)  
cleaned\_options = convert\_booleans(options.stringify\_keys.reject  
{|key, value| value.nil?})  
’ ’ + cleaned\_options.map {|key, value|  
%(#{key}="#{value.to\_s}")}.sort \* ’ ’ unless cleaned\_options.empty?  
end

After all, it’s easy enough to put h() around your callback values if  
you want it. However, doing so might break some existing code, so I’d  
like peoples opinions on alternatives before I submit a patch.

I also started a ticket on Trac, which I will update with this info.

Any input from more experienced Ruby/Rails folks would be greatly  
appreciated.

Thanks,

-Mike.

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [April 20, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/javascriptgenerator-dead-end/58273/5 "2006-04-20T20:26:16Z")

</div>

Update: Oops, the above code actually works correctly. However, if you  
change it to:

\<% @foo=‘bar’\>  
\<%= link\_to\_remote :url=\>{:parial =\> ‘foo’},  
:update =\> ‘bar’,  
:oncomplete =\> update\_page do |page|  
page.alert("’#{@foo}’")  
end  
%\>

you will get the escaped quotes. Shouldn’t this be considered a bug?  
It certainly limits the usefulness of update\_page.

I’ve continued my digging through the code, but still can’t figure out  
where this is happening. Any help would be greatly appreciated.

Thanks much,

-Mike.
