开发者

Issues with escaped html in .js.erb file

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

开发者_JAVA技巧

$("#<%= "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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜