Skip to content

Instantly share code, notes, and snippets.

@kinduff
Created October 5, 2023 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinduff/e512fb88bdb1be1da8503c7373b6bbb0 to your computer and use it in GitHub Desktop.
Save kinduff/e512fb88bdb1be1da8503c7373b6bbb0 to your computer and use it in GitHub Desktop.
Code preview for the updated sorting example in Ransack
<%= search_form_for @q do |f| %>
<%= f.label :title_cont %>
<%= f.search_field :title_cont %>
<%= f.submit "Search" %>
<% end %>
<table>
<thead>
<tr>
<th><%= sort_link(@q, :title, "Title") %></th>
<th><%= sort_link(@q, :category, "Category") %></th>
<th><%= sort_link(@q, :created_at, "Created at") %></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.category %></td>
<td><%= post.created_at.to_s(:long) %></td>
</tr>
<% end %>
</tbody>
</table>
class PostsController < ActionController::Base
def index
@q = Post.ransack(params[:q])
@q.sorts = ['title asc', 'created_at desc'] if @q.sorts.empty?
@posts = @q.result(distinct: true)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment