使用AJAX重新加载部分视图无法正常工作

时间:2014-01-16 16:22:32

标签: jquery ruby-on-rails ruby-on-rails-3.1

我想要在不重新加载页面的情况下对列进行排序,我正在尝试使用Ajax,但它无法正常工作。

welcome_controller.rb

def index
  @keyword = params[:keyword]
  @category = params[:category]

  @search = HrCurriculumIntern.search(params[:search])
  @hr_curriculum_interns = @search.all

  respond_to do |format|
    format.js
    format.html
  end
end

视图/欢迎/ index.html.erb

<% title %>

<div class="container">
  <ul class="tabs" data-tabs data-toggle="tab">
<li class="active"><a href="#main"><%= t('labels.main') %></a></li>
<li><a href="#interns"><%= t('labels.interns') %></a></li>
<li><a href="#analysts"><%= t('labels.analysts') %></a></li>
</ul>  

<div class="tab-content"> 
 <div id="main" class="active">
  <%= render "search" %>
</div>

<div id="interns" >
  <%#= render "hr_curriculum_interns/hr_curriculum_interns_filter" %>
  <%= render "hr_curriculum_interns/table_hr_curriculum_interns" %>
</div>

<div id="analysts" >
  <%= render "hr_curriculum_systems_analysts/table_hr_curriculum_systems_analysts" %>
</div>

在views / welcome / index.js.erb

$("#table_hr_curriculum_interns").html("<%=escape_javascript(render("hr_curriculum_interns/table_hr_curriculum_interns")) %>");

视图/ hr_curriculum_interns / _table_hr_curriculum_interns.html.erb

<%= stylesheet_link_tag "bootstrap" %>
<%= javascript_include_tag "bootstrap-tabs" %>

 <div class="container">
 <%= new_button HrCurriculumIntern.new, [:new, :hr_curriculum_intern] %>
   <table class="zebra-striped">
      <thead>
        <tr>
    <th><%= sort_link @search, :date_interview, t('attributes.date_interview') %></th>
    <th><%= sort_link @search, :name, t('attributes.name') %></th>
    <th><%= sort_link @search, :college, t('columns.college') %></th>
    <th><%= sort_link @search, :major, t('columns.major') %></th>      
    <th><%= sort_link @search, :answer_sent, t('columns.answer_sent') %></th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
</thead>
<tbody>
  <%# @hr_curriculum_interns = HrCurriculumIntern.all %>
  <% @hr_curriculum_interns.each do |hr_curriculum_intern| %>
    <tr>
      <td><%= display_date(hr_curriculum_intern.date_interview) %></td>
      <td><%= smart_truncate(hr_curriculum_intern.name) %></td>     
      <td><%= smart_truncate(hr_curriculum_intern.college) %></td>
      <td><%= smart_truncate(hr_curriculum_intern.major) %></td>
      <td><%= hr_curriculum_intern.answer_sent %></td>
      <td><%= show_icon hr_curriculum_intern, hr_curriculum_intern, "data-default-action" => true %></td>
      <td> <%= edit_icon hr_curriculum_intern, edit_hr_curriculum_intern_path(hr_curriculum_intern) %></td>
      <td><%= link_to image_tag('icons/destroy.png'), hr_curriculum_intern, method: :delete, data: { confirm: t('messages.are_you_sure') } %></td>
    </tr>
  <% end %>
</tbody>

这不是workinks,当我点击任何列名称时,页面会重新加载。 怎么了?

1 个答案:

答案 0 :(得分:0)

为什么只需重新排序数据就需要执行AJAX请求?为什么不对它进行排序并使用客户端JS重新显示它?

有几个Railscast会告诉你如何做到这一点:

http://railscasts.com/episodes/340-datatables

http://railscasts.com/episodes/228-sortable-table-columns