gpt4 book ai didi

ruby - 如何通过AJAX进行搜索排序?

转载 作者:数据小太阳 更新时间:2023-10-29 08:47:39 25 4
gpt4 key购买 nike

我正在使用 Rails 3.2 和 Ruby 1.9.3,并使用 Ransack 0.6.0。

我的应用程序中有一个名为 Activities 的模型,我通过 AJAX 和 Ransack(排序和搜索)部分显示该模型数据。

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

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

这是我的 Controller 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.

关于ruby - 如何通过AJAX进行搜索排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22862860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com