在这里,我想通过 ajax 调用 _employee_registers.html.erb。只有搜索输出将使用 ajax 显示。我正在关注这个http://railscasts.com/episodes/240-search-sort-paginate-with-ajax。它运作良好。但我现在想在我的项目中实现它。
我的 index.html.erb
<%= link_to 'New Employee register', new_employee_register_path %>
<%= form_tag employee_registers_path, :method => 'get', :id => "employee_registers_search" do %>
<p>
<%= select_tag "fieldtype", options_for_select([ "name", "email" ], "name") %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil%>
</p>
<div id="employee_registers"><%= render 'employee_registers' %></div>
<% end %>
<br />
我的 index.js.erb
$("#employee_registers").html("<%= escape_javascript(render("employee_registers")) %>");
我的 _employee_registers.html.erb 部分
<h1>Listing employees</h1>
<% @employee_registers.each do |employee_register| %>
<div class="span2 curve">
<div class="profile_img">
<%= image_tag employee_register.image.url,:size => "150x180"%>
</div>
<p><strong><%= employee_register.first_name %><%= employee_register.last_name %>
<br>
<%= employee_register.department.name%><%=employee_register.division.name%><br>
<%=mail_to employee_register.wemail%><br></strong></p>
%><br/></br/>
<% end %>
</div>
<br />
我的 application.js
$(function() {
$("#employee_registers th a").live("click", function() {
$.getScript(this.href);
return false;
});
$("#employee_registers_search input").keyup(function() {
$.get($("#employee_registers_search").attr("action"), $("#employee_registers_search").serialize(), null, "script");
return false;
});
我的问题是,而不是只 refresing 那个 div
$("#employee_register").html
它正在提供所有页面的输出,并且 ajax 不起作用。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/bootstrap.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/bootstrap-wysihtml5/core.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/bootstrap-wysihtml5/index.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/bootstrap-responsive.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/companies.css?body=1" media="all" rel="stylesheet" type="text/css" />
<script media="all" src="/assets/bootstrap.js?body=1" type="text/javascript"></script>
可能是什么问题呢?