这里我想调用_employee_registers.html。Erb通过ajax。只有搜索输出将使用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;
});
我的问题不是只刷新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>
有什么问题吗?
我能看到两件事,
1)使您的表单remote => true
<%= form_tag employee_registers_path, :method => 'get', :remote => true, :id => "employee_registers_search" do %>
2)它的小不寻常,你有用户列表在搜索表单本身,试着让它外面的form
标签(这与你的错误无关,只是作为一种做法)
HTH
你的代码有几个问题。
我认为主要的问题是,你希望你的表单给employee_registers基于搜索,但你没有从搜索动作的javascript响应。
假设你有一个像这样的搜索操作
def search
@employee_registers = Employee.where(:name => params[:search])
end
然后在你的search.js.erb文件
$("#employee_registers").append("<%= j(render @employee_registers) %>
(j()是escape_javascript()的别名)
在你的_employee_registers.html中。erb部分
<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/>
</div>
<br />
现在您必须更改index.html.erb文件
中的一些代码<%= link_to 'New Employee register', new_employee_register_path %>
<%= form_tag employee_registers_path, , :remote => true, :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"></div>
<% end %>
<br />
我没有测试过这个,可能在你的代码的其他地方有错误。你的问题包含了很多代码,你解释了错误是什么,这很好。但是,不清楚你想做什么。试着多解释一点,你会得到更好的答案。