Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 using ransack to search and sort and trying to limit options in sort select
    primarykey
    data
    text
    <p>I have a page in my rails application that shows a list of listings (paginated using will_paginate). I am trying to find a good gem to implement searching and sorting with the list on this page. The Ransack gem looked like what i needed but i'm running into an issue with it. This is what my code looks like right now with the Ransack gem.</p> <p>My model (didn't include the whole model, didn't want the validations and extra code to get in the way):</p> <pre><code>UNRANSACKABLE_ATTRIBUTES = ["id", "state", "show_email", "email", "show_phones", "primary_phone", "secondary_phone", "updated_at", "user_id"] def self.ransackable_attributes auth_object = nil (column_names - UNRANSACKABLE_ATTRIBUTES) + _ransackers.keys end </code></pre> <p>My controller action:</p> <pre><code>def index @search = Listing.search(params[:q]) @listings = @search.result.paginate(page: params[:page], per_page: 20, joins: "LEFT OUTER JOIN attachments ON listings.id = attachments.attachable_id", select: "listings.*, COUNT(attachments.id) AS photos", group: "listings.id", order: "listings.created_at DESC, listings.title ASC, listings.id DESC") @search.build_sort if @search.sorts.empty? end </code></pre> <p>My search form on the page:</p> <pre><code> &lt;div class="advanced-search"&gt; &lt;%= search_form_for @search do |f| %&gt; &lt;div class="search-by"&gt; &lt;div class="search-field"&gt; &lt;%= f.label :title_cont, "Title Contains" %&gt; &lt;%= f.text_field :title_cont %&gt; &lt;/div&gt; &lt;div class="search-field"&gt; &lt;%= f.label :description_cont, "Description Contains" %&gt; &lt;%= f.text_field :description_cont %&gt; &lt;/div&gt; &lt;div class="search-field"&gt; &lt;%= f.label :price_gteq, "Price Between" %&gt; &lt;%= f.text_field :price_gteq %&gt; &lt;%= f.label :price_lteq, "and" %&gt; &lt;%= f.text_field :price_lteq %&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="sort-by"&gt; &lt;%= f.sort_fields do |s| %&gt; &lt;%= s.sort_select %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;div class="search-actions"&gt; &lt;%= f.submit "Search" %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p>My route:</p> <pre><code>resources :listings do collection do match 'search' =&gt; 'listings#index', via: [:get], as: :search end end </code></pre> <p>With everything i have in place now i have been able to get the application to successfully search and sort the listings.</p> <p><strong>My question:</strong> The <code>sort_select</code> field is showing everything option that is "ransackable". Is there a way to make one attribute available for the searching, but <em>not</em> the sorting? For example i want the ability to search on the <code>description</code> attribute but it doesn't make any sense to include that attribute in the sorting. How can i use an attribute in one and not the other? Thanks for any help and let me know if my question is not clear enough.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload