diff --git a/docs/docs/getting-started/sorting.md b/docs/docs/getting-started/sorting.md index dde0b7ad..c0619b1d 100644 --- a/docs/docs/getting-started/sorting.md +++ b/docs/docs/getting-started/sorting.md @@ -10,70 +10,62 @@ title: Sorting You can add a form to capture sorting and filtering options together. ```erb -
-
-
-

Filters

-
-
- -
- <%= search_form_for @q, - class: 'form', - url: articles_path, - html: { autocomplete: 'off', autocapitalize: 'none' } do |f| %> - -
- <%= f.label :title_cont, t('Filter_by_keyword') %> - <%= f.search_field :title_cont %> -
- - <%= render partial: 'filters/date_title_sort', locals: { f: f } %> - -
- <%= f.label :grade_level_gteq, t('Grade_level') %> >= - <%= f.search_field :grade_level_gteq %> -
- -
- <%= f.label :readability_gteq, t('Readability') %> >= - <%= f.search_field :readability_gteq %> -
- -
- <%= @articles.total_count %> articles -
- -
-
-
- <%= link_to request.path, class: 'form-link' do %> - <%= t('Clear_all') %> - <% end %> - - <%= f.submit t('Filter'), class: 'btn btn-primary' %> -
-
+# 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 %> + + + + + + + + + + + + <% @posts.each do |post| %> + + + + + <% end %> - - + +
<%= sort_link(@q, :title, "Title") %><%= sort_link(@q, :category, "Category") %><%= sort_link(@q, :created_at, "Created at") %>
<%= post.title %><%= post.category %><%= post.created_at.to_s(:long) %>
``` - ## 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 ```