Rails3 JQuery strange behavior

I am doing an Ajax call, rendering ‘done.js.erb’ in which I wrote a
jQuery append built from an helper current_snippet

$(’#upload_tables’).append("<%= current_snippet(@remote_locker,
@current) -%>"); RAISES AN ERROR : missing ) after argument
list

— debugging the current_snippet helper, I get an html string as
output

Ajax-loader table_instructors</
td> <a href=”/en/admin/remote_lockers/4d7b27b8a326cb033d00000b/
reloading.js?table=table_instructors" data-remote=“true”>start</
td> "

– but strange … if I copy this string into the js file (rather than
calling the snippet helper) then NO ERROR …

$(’#upload_tables’).append(“

table_instructors <a href=”/en/admin/
remote_lockers/4d7b27b8a326cb033d00000b/reloading.js?
table=table_instructors" data-remote=“true”>start ");

so the output string is well escaped … why plain string is working
well , but helper call is not …
I already used another snippet helper in a previous js line without
such problem…
$(’#<%= @previous -%>’).replaceWith("<%= previous_snippet(@previous) -
%>");
so it’s not the js line writing…

if I insert a simple link tag in the helper, rather than generating
it , ( html << “Start” ) then
$(’#upload_tables’).append("<%= current_snippet(@remote_locker,
@current) -%>"); doesn"t raise ANY ERROR

Ajax-loader table_instructors</
td> Start

it seems the ‘issue’ is in the link_to generated in the helper
link_to I18n.t(:start),
reloading_admin_remote_locker_path(remote_locker, :table =>
current, :format => :js), :remote => true

any clue ? thanks

On 14 Mar 2011, at 17:46, Erwin [email protected] wrote:

I am doing an Ajax call, rendering ‘done.js.erb’ in which I wrote a
jQuery append built from an helper current_snippet

$(‘#upload_tables’).append(“<%= current_snippet(@remote_locker,
@current) -%>”); RAISES AN ERROR : missing ) after argument
list

You’re not escaping the output of current snippet - if it contains any "
then that will screw up stuff

If you inspect the output of current_snippet the it will print "
characters as " but that’s just how it displays stuff - it doesn’t mean
that any escaping has actually taken place. There’s an escape_javascript
(or something similarly named) method in rails

Fred

[SOLVED] after playing with the snippet generation , I finally
resolved to modify the way the link_to was generated
by writing myself the link tag …

path = reloading_admin_remote_locker_path(remote_locker, :table =>

current, :format => :js)
html << ‘<a href=’ + "#{path} data-remote=‘true’>#{I18n.t(:start)}
"

I can even write it in one line… but I’ll have a look at the
escape_javascript as suggested… thanks a lot