ruby - How to do ransack sorting through AJAX? -
i working rails 3.2 ruby 1.9.3 , using ransack 0.6.0.
i have model called activities in application , displaying model data in partial through ajax ransack (sorting , searching).
when click on of sort_link
of field, result displayed in new page not in partial. need display result in same partial without refreshing page.
here model 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
here 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
here view _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> <% 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 |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 sure?'%> </td> </tr> <% end %> </table>
updated
i got solution problem
to working sort_link through ajax
, can use
<%= sort_link(@q, :date, {:type => @type, :state => @state, :status => @status, :due_date => @due_date}, { :remote => true, :method => :post } ) %>
and need write _tasks.js.erb
_tasks.js.erb
$('.display').html("<%= j render(partial: 'task') %>") #here .display class name want display result.
Comments
Post a Comment