在Rails中使用Ajax作为整个页面呈现

时间:2013-01-15 09:33:19

标签: ruby-on-rails ruby ruby-on-rails-3 jquery ruby-on-rails-3.2

这里我想通过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")) %>");

my _employee_registers.html.erb partial

<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>

可能是什么问题?

2 个答案:

答案 0 :(得分:1)

我可以看到两件事,

1)制作表格remote => true

<%= form_tag employee_registers_path, :method => 'get', :remote => true, :id => "employee_registers_search" do %>

2)你在搜索表单中有用户列表,这很不寻常,试着把它放在form标签之外(这与你的错误无关,就像练习一样)

HTH

答案 1 :(得分:1)

您的代码存在一些问题。

我认为主要问题是您希望表单根据搜索提供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 />

我没有对此进行测试,代码中的其他地方可能存在错误。你的问题包含很多代码,你解释错误是什么,这很好。但是,目前尚不清楚你想做什么。尝试解释一下,你会得到更好的答案。