English 中文(简体)
Filtering a Table in Rails
原标题:

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?

Thank you!

index controller:

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 => conditions)
 @videos = Video.paginate :page => params[:page], :per_page => 100, :order => sort, :conditions => conditions
if request.xml_http_request?
  render :partial => "video_list", :layout => false
end

Index view:

<form name="sform" action="" style="display:inline;">
<br>
<label for="item_name">Search by  <%= select_tag "filter", "<option>Title</option>,<option>Size</option>,<option>Film Type</option>,<option>Premiere</option>,<option>Preferred Date</option>,<option>Actual Date</option>" %>: </label>
<%= text_field_tag(:query, params[ query ], :size => 10, :id =>  query ) %>

</form>

<%= image_tag("spinner.gif",
              :align => "absmiddle",
              :border => 0,
              :id => "spinner",
              :style =>"display: none;" ) %>
</p>

<%= observe_form  sform ,  :frequency => 1,
         :update =>  table ,
         :before => "$( #spinner ).show()",
         :success => "$( #spinner ).hide()",
         :url => {:action =>  list },
         :with =>   sform   %>


<div id="table">
<%= render :partial => "video_list" %>
</div>

Still cannot figure out how to make it search by the filter type. Any more suggestions?

问题回答

In the last part there:

conditions = ["title LIKE ?", "%#{params[:query]}%"] unless params[:query].nil?

The conditions you re passing explicitly are searching by title. Is this what you want to do?





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签