Adt
Adt

Reputation: 495

SyntaxError: Unexpected token true in html generated by ruby

Why I am getting this error: SyntaxError: Unexpected token true in line

$('#atp_13').append("<a data-remote="true" rel="nofollow" data-method="delete" href="/clots/test/unjoin"><i class='icon-check clot-unfollow-icon'></i></a>)");

i m generating this using

$('#<%= "atp_#{@atp.id}" %>').append("<%= link_to unjoin_atp_path(@atp), :method => :delete, remote: true do %><i class='icon-check clot-unfollow-icon'></i><% end %>)");

Upvotes: 1

Views: 811

Answers (2)

Sasidaran
Sasidaran

Reputation: 454

$("#atp_<%[email protected] %>").append("<%= escape_javascript(link_to unjoin_atp_path(@atp), :method => :delete, remote: true do %><i class='icon-check clot-unfollow-icon'></i><% end %>)");

escape_javascript is used to escape the rails helper methods to be replaced in the page.

You can move the link_to tag to a partial file to make it clean, and you can call the partial name in the js.erb file.

$("#atp_<%[email protected] %>").append("<%= escape_javascript(render(:partial => 'partial_name') ) %>");

Upvotes: 2

mylee
mylee

Reputation: 1333

Because the " character is not properly escaped

"<a data-remote="true" rel="nofollow" data-method="delete" href="/clots/test/unjoin"><i class='icon-check clot-unfollow-icon'></i></a>)"

vs.

"<a data-remote=\"true\" rel=\"nofollow\" data-method=\"delete\" href=\"/clots/test/unjoin\"><i class='icon-check clot-unfollow-icon'></i></a>)"

Notice the colour of true in both examples. The first example is like declaring a variable this way:

var a = "something is "true;

Upvotes: 2

Related Questions