escape_javascript is removing angle brackets - Solutions?
I am outfitting a rails 3 app (version 3.0.9) to use AJAX and jQuery to fluidly handle user posts. I would like for a #users_info div to be refreshed following the post submission. I can get this to work, but the contents of the div don't render properly. Specifically, the jquery code in create.js.erb:
$("#right_bar").html(
'<%= escape_javascript(render :partial => 'shared/user_info') %>'
);
Results in the user info div outputting:
<div id=user_info> <h1>
<a href=/users/101>Example Usera> h1>
46 posts <br>
4 discussions <br>
Following 2 topics <br>
Joined 8 days ago.div>
Note how the escape_javascript function removes all leading angle brackets on end tags in html ( becomes a>, become h1>). How can I force the escape_javascript to avoid doing so? I think this is all the relevant code, but can post more if needed.
UPDATE
I was wondering if maybe using to_json would be part of the solution. the code:
<%= escape_javascript((render :partial => 'shared/user_info').to_json) %>
results in the html tags not being destroyed. However, i don't know how to convert back from json to output the desired html. Just thought this might be the right start, but i'm not sure how t开发者_如何学JAVAo finish it
You're probably on an older version of rails. A release earlier in June caused this problem. Make sure to upgrade to the current release in the branch you're on (2.3.12, 3.0.9, or 3.1.0.rc4) and it should work.
I found a solution. just wrap the contents of escape_javascript(contents) in escape_javascript("#{}").
$("#right_bar").html(
'<%= (escape_javascript("#{(render :partial => 'shared/user_info')}")).html_safe %>'
);
I added the html_safe at the end so that rails would not make the angle brackets literal. Now the html tags go through properly, and everything renders properly in the browser.
Try this:
$("#right_bar").html(
"<%= escape_javascript(render :partial => 'shared/user_info') %>"
);
精彩评论