Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Updates sorting example #1442

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 45 additions & 53 deletions docs/docs/getting-started/sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,62 @@ title: Sorting
You can add a form to capture sorting and filtering options together.

```erb
<div class="filters" id="filtersSidebar">
<header class="filters-header">
<div class="filters-header-content">
<h3>Filters</h3>
</div>
</header>

<div class="filters-content">
<%= search_form_for @q,
class: 'form',
url: articles_path,
html: { autocomplete: 'off', autocapitalize: 'none' } do |f| %>

<div class="form-group">
<%= f.label :title_cont, t('Filter_by_keyword') %>
<%= f.search_field :title_cont %>
</div>

<%= render partial: 'filters/date_title_sort', locals: { f: f } %>

<div class="form-group">
<%= f.label :grade_level_gteq, t('Grade_level') %> >=
<%= f.search_field :grade_level_gteq %>
</div>

<div class="form-group">
<%= f.label :readability_gteq, t('Readability') %> >=
<%= f.search_field :readability_gteq %>
</div>

<div class="form-group">
<i><%= @articles.total_count %> articles</i>
</div>

<div class="form-group">
<hr/>
<div class="filters-header-content">
<%= link_to request.path, class: 'form-link' do %>
<i class="far fa-undo icon-l"></i><%= t('Clear_all') %>
<% end %>

<%= f.submit t('Filter'), class: 'btn btn-primary' %>
</div>
</div>
# app/views/posts/index.html.erb

<%= 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 %>
</div>
</div>
</tbody>
</table>
```


## Sorting in the Controller

To specify a default search sort field + order in the controller `index`:

```ruby
@search = Post.ransack(params[:q])
@search.sorts = 'name asc' if @search.sorts.empty?
@posts = @search.result.paginate(page: params[:page], per_page: 20)
# app/controllers/posts_controller.rb
class PostsController < ActionController::Base
def index
@q = Post.ransack(params[:q])
@q.sorts = 'title asc' if @q.sorts.empty?

@posts = @q.result(distinct: true)
end
end
```

Multiple sorts can be set by:

```ruby
@search = Post.ransack(params[:q])
@search.sorts = ['name asc', 'created_at desc'] if @search.sorts.empty?
@posts = @search.result.paginate(page: params[:page], per_page: 20)
# app/controllers/posts_controller.rb
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
```
Loading