JayX
JayX

Reputation: 1784

Rails escape_javascript is not rendering stuff

Here is my code

$(function(){
$('#new_post').bind('ajax:success', function() {
    $('.posts').append("<%=escape_javascript(render :partial=>'post', :loca\
ls=>{:post=>Post.new})%>");
    });
});

which results in a bizarre output, the page simply append <%=escape_javascript(render :partial=>'post', :locals=>{:post=>Post.new})%> to my list.

Does anyone know how this could happen? Thanks!

Upvotes: 1

Views: 3294

Answers (2)

JayX
JayX

Reputation: 1784

I solved the problem eventually, thanks!

Actually I used that approach first, but it didn't work.

So it turns out I used

format.js { render :nothing => true }  

in my controller, which will probably prevent javascript from rendering anything.

Besides, instead of using

<%= div_for post do %>

I used

<tr><td> 

format for my single post and I guess that's part of reason why my javascript doesn't work

now my create.js.erb only renders

$('#posts').append("<%= escape_javascript(render(@post)) %>");

and it works.

Thank you guys, I am new to this community (also rails/javascript) but I have learned a lot already.

Upvotes: 2

tadman
tadman

Reputation: 211590

If this is a partial or a view, make sure it is named with the extension .js.erb or you may not get the proper ERB interpolation happening.

Upvotes: 1

Related Questions