Forum: Ruby on Rails update_page yields escaped code in callback

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mike E. (Guest)
on 2006-04-18 10:28

Hi All,

The subject pretty well describes what I am experiencing.  This line in
an .rhtml template:

<%= link_to_remote  menu_item.humanize,
  {:update => 'section',
   :url =>{:controller => menu_item},
   :complete => update_page do |page|
     page.activate_tab("#{menu_item}_tab", 'menu_')
     page['section'].hide
     page['section'].visual_effect('Appear', '{queue: \'end\'}')
    end
   },
:id => "#{menu_item}_tab", :class => 'menu_tab' %>

yields this godawful mess (the 'activate_tab' method is in the
ApplicationHelper, but all the javascript is being escaped regardless of
where it's generated):

<a class="menu_tab" href="#" id="contacts_tab" onclick="new
Ajax.Updater('section', '/contacts', {asynchronous:true,
evalScripts:true, onComplete:function(request){try {
$$(&quot;menu_tab&quot;).each(function(value, index) {
value.removeClassName(&quot;current&quot;);
});
$(&quot;contacts_tab&quot;).add_class_name = &quot;current&quot;;
$(&quot;section&quot;).hide();
$(&quot;section&quot;).visualEffect(&quot;Appear&quot;, &quot;{queue:
'end'}&quot;);
} catch (e) { alert('RJS error:\n\n' + e.toString());
alert('$$(\&quot;menu_tab\&quot;).each(function(value, index)
{\nvalue.removeClassName(\&quot;current\&quot;);\n});\n$(\&quot;contacts_tab\&quot;).add_class_name
=
\&quot;current\&quot;;\n$(\&quot;section\&quot;).hide();\n$(\&quot;section\&quot;).visualEffect(\&quot;Appear\&quot;,
\&quot;{queue: \'end\'}\&quot;);'); throw e }}}); return
false;">Contacts</a>


I looked through the docs and even scanned the code a bit, but I can't
figure out where escape_javascript is being called or how to prevent it
from being called.

Any help would be greatly appreciated.

Thanks much,

-Mike.
Jeff C. (Guest)
on 2006-04-18 11:37
Mike Evron wrote:
>

> I looked through the docs and even scanned the code a bit, but I can't
> figure out where escape_javascript is being called or how to prevent it
> from being called.
>
> Any help would be greatly appreciated.
>
> Thanks much,
>
> -Mike.

Try removing the :update parameter from your link_to_remote call, see if
that has any effect.

Jeff C.man
Alan F. (Guest)
on 2006-04-18 18:30
Mike Evron wrote:
>
>
> Hi All,
>
> The subject pretty well describes what I am experiencing.  This line in
> an .rhtml template:
>
> <%= link_to_remote  menu_item.humanize,
>   {:update => 'section',
>    :url =>{:controller => menu_item},
>    :complete => update_page do |page|
>      page.activate_tab("#{menu_item}_tab", 'menu_')
>      page['section'].hide
>      page['section'].visual_effect('Appear', '{queue: \'end\'}')
>     end
>    },
> :id => "#{menu_item}_tab", :class => 'menu_tab' %>

I think the :complete takes a javascript function *name*, rather than
the whole function.

Alan
Alan F. (Guest)
on 2006-04-18 18:34
Alan F. wrote:
> Mike Evron wrote:
>>
>>
>> Hi All,
>>
>> The subject pretty well describes what I am experiencing.  This line in
>> an .rhtml template:
>>
>> <%= link_to_remote  menu_item.humanize,
>>   {:update => 'section',
>>    :url =>{:controller => menu_item},
>>    :complete => update_page do |page|
>>      page.activate_tab("#{menu_item}_tab", 'menu_')
>>      page['section'].hide
>>      page['section'].visual_effect('Appear', '{queue: \'end\'}')
>>     end
>>    },
>> :id => "#{menu_item}_tab", :class => 'menu_tab' %>
>
> I think the :complete takes a javascript function *name*, rather than
> the whole function.
>
> Alan

e.g. :complete => "undoRequestCompleted(request)" or something.

You might try:

--------------
<script type = "text/javascript>
function mycomplete()
{
<%= update_page do |page|
      page.activate_tab("#{menu_item}_tab", 'menu_')
      page['section'].hide
      page['section'].visual_effect('Appear', '{queue: \'end\'}')
     end %>
}
</script>
-----------------

to use rjs to generate the javascript into a regular function, which you
then pass to complete

Alan
and using :complete => "mycomplete"
Mike E. (Guest)
on 2006-04-18 20:49
Wow!  You guys are right.  It's been escaping my manual javascript the
whole time and I didn't even notice it, because it worked! And who would
have though such badly mangled code would work...  The only thing busted
is effects queues

Guess I have to restructure a couple of things in my app unless somebody
can figure out how to make it stop doing that.  There really should be a
way...

Thanks for all the help.

>> I think the :complete takes a javascript function *name*, rather than
>> the whole function.
>>
>> Alan
>
> e.g. :complete => "undoRequestCompleted(request)" or something.
>
> You might try:
>
> --------------
> <script type = "text/javascript>
> function mycomplete()
> {
> <%= update_page do |page|
>       page.activate_tab("#{menu_item}_tab", 'menu_')
>       page['section'].hide
>       page['section'].visual_effect('Appear', '{queue: \'end\'}')
>      end %>
> }
> </script>
> -----------------
>
> to use rjs to generate the javascript into a regular function, which you
> then pass to complete
>
> Alan
> and using :complete => "mycomplete"
This topic is locked and can not be replied to.