Note that there are some explanatory texts on larger screens.

plurals
  1. POFiltering a Table in Rails
    text
    copied!<p>I am working with will_paginate to create a sortable and searchable table. I have got everything to work correctly with the exception of the search box (filter). Right now when I select a different query (other than "title") and search for something, it only searches for title querys. Can anyone help point out where I am going wrong?</p> <p>Thank you!</p> <p>index controller:</p> <pre><code>filter = case params['sform'] when "filter=Title" then "title" when "filter=Size" then "size" when "filter=Film Type" then "film_type" when "filter=Premiere" then "premiere" when "filter=Preferred Date" then "preferred_date" when "filter=Actual Date" then "actual_date" end sort = case params['sort'] when "title" then "title" when "premiere" then "premiere" when "film_type" then "film_type" when "preferred_date" then "preferred_date" when "actual_date" then "actual_date" when "created_at" then "created_at" when "updated_at" then "updated_at" when "size" then "size" when "title_reverse" then "title DESC" when "premiere_reverse" then "premiere DESC" when "film_type_reverse" then "film_type DESC" when "preferred_date_reverse" then "preferred_date DESC" when "actual_date_reverse" then "actual_date DESC" when "created_at_reverse" then "created_at DESC" when "updated_at_reverse" then "updated_at DESC" when "size_reverse" then "size DESC" end conditions = ["title LIKE ?", "%#{params[:query]}%"] unless params[:query].nil? @total = Video.count(:conditions =&gt; conditions) @videos = Video.paginate :page =&gt; params[:page], :per_page =&gt; 100, :order =&gt; sort, :conditions =&gt; conditions if request.xml_http_request? render :partial =&gt; "video_list", :layout =&gt; false end </code></pre> <p>Index view:</p> <pre><code>&lt;form name="sform" action="" style="display:inline;"&gt; &lt;br&gt; &lt;label for="item_name"&gt;Search by &lt;%= select_tag "filter", "&lt;option&gt;Title&lt;/option&gt;,&lt;option&gt;Size&lt;/option&gt;,&lt;option&gt;Film Type&lt;/option&gt;,&lt;option&gt;Premiere&lt;/option&gt;,&lt;option&gt;Preferred Date&lt;/option&gt;,&lt;option&gt;Actual Date&lt;/option&gt;" %&gt;: &lt;/label&gt; &lt;%= text_field_tag(:query, params['query'], :size =&gt; 10, :id =&gt; 'query') %&gt; &lt;/form&gt; &lt;%= image_tag("spinner.gif", :align =&gt; "absmiddle", :border =&gt; 0, :id =&gt; "spinner", :style =&gt;"display: none;" ) %&gt; &lt;/p&gt; &lt;%= observe_form 'sform', :frequency =&gt; 1, :update =&gt; 'table', :before =&gt; "$('#spinner').show()", :success =&gt; "$('#spinner').hide()", :url =&gt; {:action =&gt; 'list'}, :with =&gt; 'sform' %&gt; &lt;div id="table"&gt; &lt;%= render :partial =&gt; "video_list" %&gt; &lt;/div&gt; </code></pre> <p>Still cannot figure out how to make it search by the filter type. Any more suggestions?</p>
 

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