Created
October 5, 2023 13:55
-
-
Save kinduff/e512fb88bdb1be1da8503c7373b6bbb0 to your computer and use it in GitHub Desktop.
Code preview for the updated sorting example in Ransack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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