&...">

Issues with escaped html in .js.erb file

I have a .js.erb file that has the following line

$ ("# <% =" User # {current_user.id} _following "%> & ") HTML (. ";% = Escape_javascript (link_to (form plural (@ count, 'label'), user_tags_path (current_user) ))%> ")

Is displayed

<a href=/users/1/tags>1 taga> 

Instead

1 tag

This worked when I started rails 3.0.4, but now that does not mean that I run rails 3.0.8. I found that I need to add .html_safe to other places that I used auto_link , or I would get the same problem.

How can I get it so that it does not escape this html?

+4
source share
1 answer

I never liked to interpolate your ruby ​​JS like this. This always makes hairiness difficult to understand.

Instead, send your data to JS in your own JSON, and then just use the data as your own 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 much more clear and completely avoids any HTML problems, since HTML is not in your ruby ​​expression at all.

+1
source

All Articles