1

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!

0

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

You mean 'post' (post.js.erb)? I put the code above in application.js, it works for my 'delete', so I assume the same approach should work for 'create'.
If you have ERB in your JavaScript, you cannot put it in public/application.js since this is sent directly to the browser without interpolation. What you need is to create a route to an action where you return the proper interpolated script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.