Reputation: 3717
I have a .js.erb file that has the following line
$("#<%= "user#{current_user.id}_following" %>").html("<%= escape_javascript(link_to(pluralize(@count,'tag'), user_tags_path(current_user))) %>")
It displays
<a href=/users/1/tags>1 taga>
Instead of
1 tag
This used to work when I was running rails 3.0.4 but now it doesn't now that I'm running rails 3.0.8. I found that I needed to add .html_safe
in other places that I had been using auto_link
or I would get the same problem.
How do I get it so that it won't escape this html?
Upvotes: 4
Views: 953
Reputation: 187004
I never liked interpolating your JS with ruby like this. It always leads to hard to understand hairyness.
Instead, dump your data into JS on it's own as JSON, then simply use the data as native javascript.
var user = <%= {
:id => current_user.id,
:path => user_tags_path(current_user),
:link_text => pluralize(@count,'tag'),
}.to_json %>;
var link = $('<a>').attr('href', user.path).text(user.link_text);
$('#user' + user.id + '_following').html(link);
Which, in my opinion, is far more understandable and completely dodges any HTML issues since the HTML is not in your ruby expression at all.
Upvotes: 1