【问题标题】:How to do ransack sorting through AJAX?如何通过AJAX进行ransack排序?
【发布时间】:2014-04-04 12:29:39
【问题描述】:

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

我的应用程序中有一个名为“活动”的模型,我正在通过带有 Ransack 的 AJAX 部分显示此模型数据(排序和搜索)。

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

这是我的模型activity.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

这是我的控制器activity_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>

【问题讨论】:

    标签: ruby ajax ruby-on-rails-3


    【解决方案1】:

    更新

    我的问题得到了解决方案

    要通过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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多