如何通过 AJAX 进行洗劫分类



我正在使用带有Ruby 1.9.3的Rails 3.2,并且正在使用Ransack 0.6.0。

我的应用程序中有名为 Activity 的模型,我正在通过 AJAX 使用 Ransack(排序和搜索)部分显示此模型数据。

当我单击任何sort_link字段时,结果将显示在新页面中,但不显示在该部分中。我需要在不刷新页面的情况下以相同的部分显示该结果。

这是我的模型活动.rb:

class Activity < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  belongs_to :folder
  belongs_to :organization
  has_many :attachments
  attr_accessible :date, :info, :tags, :attachment, :category_id, :priority, :assigned_to, :notify, :activity_type, :task_desc, :task_status, :due_date, :task_state, :folder_id, :attachments_count, :attachments_attributes, :user_id, :organization_id
end

这是我的控制器 activities_controller.rb:

 def tasks
    @type = params[:type]
    @state = params[:state]
    @status = params[:status].split(',') if !params[:status].nil?
    @due_date = change_date_format1(params[:date]) if !params[:date].blank?
      @q = current_user.user_activities(@type, @state, @status, @due_date).search(params[:q])
     if params[:sel]
      @activities = @q.result(:distinct  => true, :order => 'asc').page(params[:page]).per(params[:sel])
      @choosed = params[:sel]
    else
  @activities = @q.result(:distinct  => true).page(params[:page]).per(100)
    @choosed = 100
    end 
    render :partial => "tasks", :layout => false
  end

这是我_tasks.html.erb的看法:

<table class="table table-striped">
      <tr>
        <th><%= sort_link(@q, :date, {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
        <th><%= sort_link(@q, :category_folder_name, "Drive", {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
        <th><%= sort_link(@q, :category_name, "Folder", {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
        <th><%= sort_link(@q, :info, "Info", {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
        <% if @type == "Task" || @type == '' %>
            <th><%= sort_link(@q, :task_state, "Task State", {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
            <th><%= sort_link(@q, :task_status, "Task Status", {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
            <th><%= sort_link(@q, :due_date, "Due Date", {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
        <th><%= sort_link(@q, :activity_type, {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
       <th><%= sort_link(@q, :assigned_to, "Assigned User", {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
          <% end %>
          <th><%= sort_link(@q, :priority, {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
          <th><%= sort_link(@q, :tags, {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
        <%# if @type == "Docs" %>
      <th><%= sort_link(@q, :attachments, "Attachment", {:type => @type, :state => @state, :status => @status, :due_date => @due_date}) %></th>
      <%# end %>
        <th></th>
      </tr>
    <% for activity in @activities %>
      <tr>
        <td class="span2"><%= activity.date.strftime("%b-%d-%Y") %></td>
 <% if !activity.folder.nil? %>
                <td class="span1"><%= @drive = activity.folder.try(:name) %></td>
        <% else %>
               <td class="span1"><%= @drive = Folder.find_by_id(Category.find_by_id(activity.category_id).folder_id).try(:name) %></td>
        <% end %>  
              <td class="span1"><%= @cat = activity.category.try(:name) %></td>
        <td class="span5">
            <% if activity.attachment? %>
              <i class="icon-file"></i>
           <% end %>
            <%= activity.info %>
        </td>
        <% if @type == "Task" || @type == '' %>
          <td class="span2"><%= activity.task_state %></td>
          <td class="span3">
           <% if !@status.class == "String" %>
          <% if !/(#{@status.join(',')})/.match(activity.task_status).nil? %>
            <%= /(#{@status.join(',')})/.match(activity.task_status) %>
          <% else %>
          <%= activity.task_status %>
          <% end %>
          <% else %>
              <%= activity.task_status %>
          <% end %>
         </td>
           <td class="span2"><%= activity.due_date.strftime("%b-%d-%Y") if !activity.due_date.blank? %></td>
        <td class="span1"><%= activity.activity_type %></td>
       <td class="span2"><%= activity.assigned_to if !activity.assigned_to.nil? %></td>
        <% end %>
        <td><%= activity.priority %></td>
        <td><%= activity.tags %></td>
        <td>
        <% if @type == "Docs" %>
      <% unless activity.attachments.blank? %>
      <% activity.attachments.each do |a| %>
      <%= link_to File.basename(a.file.path), a.file.url if !a.file.nil? %>
    <% end %>
    <% else %>
      <%= activity.attachment if !activity.attachment.nil? %>
    <% end %>
    </td>
    <% end %> 
        <td class="span3">
          <%= link_to "Detail", activity_path(activity), class: 'btn btn-mini'%>
          <%= link_to "Edit", edit_activity_path(activity, folder: @drive, category: @cat), class: 'btn btn-mini'%>
          <%= link_to "Delete", activity_path(activity), class: 'btn btn-mini btn-danger', method: :delete, confirm: 'Are you sure?'%>
        </td>
      </tr>
    <% end %>
    </table>

已更新

我得到了我的问题的解决方案

要通过AJAX与sort_link一起工作,我们可以使用

<%= sort_link(@q, :date, {:type => @type, :state => @state, :status => @status, :due_date => @due_date}, { :remote => true, :method => :post } ) %>

然后你需要写_tasks.js.erb

_tasks.js.erb

$('.display').html("<%= j render(partial: 'task') %>")  #here .display is a class name where you want to display the result.

相关内容

  • 没有找到相关文章

最新更新