简体   繁体   中英

Ruby on Rails3: execute rails code into js.erb view, does not work

I want to do kaminari paginator dynamically

This code, handle click event to Kaminari links

Kaminari.js

$(document).ready(function() {
  $(".paginator a").on("click", function() {
    $.getScript(this.href);
    return false;
  });
});

users_controller:

def index
  @users = User.all(:order => "name")
  @users = Kaminari.paginate_array(@users).page(params[:page]).per(10)
end

index.html.erb

<h1>Users</h1>
<div id="users-page">
  <%= render 'users'  %>
</div>

<%= paginate @users %>

Then I wrote a js.erb view for users dynamically pagination:

app/views/users/index.js.erb

If I only put javascript code like

alert("paging");

when I click on paginations links, alert message perfectly.

But if I put <%= ... %> code inside, this javascript response does not execute.

alert("paging");
$("#users-page").html(<%= render 'users' %>)

either the message is displayed.

which may be the problem?

This happens when javascript error exists. In this case, quotation marks missing in html() function.

$("#users-page").html("<%= escape_javascript(render ('users')) %>")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM