jQuery call for a partial doesn't work if there is RoR code in it?
I am trying to get paginating to work with jQuery. I am using the will_paginate gem. It works normally (with no javascript). I tried following a railscast (http://railscasts.com/episodes/174-pagination-with-ajax) but I am still having a problem.
show.js.erb
$('#comments').html("<%= escape_javascript(render "comments/comment.html.erb") %>");
This works when the partial file is just html. IE no <% code like this %>. Why? Is .html the wrong jQuery function to call? What should I use?
From trying lets of things, this code might be the problem:
$(".pagination a").c开发者_如何学JAVAlick(function() {
//$(".pagination").html("Page is loading...");
$.get(this.href, null, null, "script");
return false;
});
what is "script"?
Although I don't know ruby, according to the page you linked, are missing a pair of parenthesis:
$('#comments').html("<%= escape_javascript(render("comments/comment.html.erb")) %>");
I don't use ruby and I'm fairly uninitiated to jQuery, but if I'm interpreting this correctly, you're trying to add HTML to an element entirely on the client side, then expecting server-side markup to be parsed within it.
This code never executes on the server. In order to get server-side markup parsed, you'd need to actually perform another server request (e.g. with XMLHttpRequest), which would perhaps process a file with this exact markup with it on the server, which would then process your server-side directives.
You'll probably be interested in jQuery's load function for this.
After much tinkering I found my problem:
I added this to my partial:
<%= will_paginate @comments %>
<% @comments.each do |comment| %>
//partial stuff
<% end %>
The screencast didn't show where he put the will_paginate line, so I assumed it stayed in the post file. It also didn't show the loop in the partial file. I didn't realise you had to do this since rails does it anyway when you call a partial file.
精彩评论