Ruby on rails 在Rails中筛选表
我正在与will_paginate合作创建一个可排序和可搜索的表。除搜索框过滤器外,我已使所有内容正常工作。现在,当我选择一个不同的查询而不是标题并搜索某个内容时,它只搜索标题查询。有人能帮我指出哪里出了问题吗 谢谢大家! 索引控制器:Ruby on rails 在Rails中筛选表,ruby-on-rails,ruby-on-rails-plugins,will-paginate,Ruby On Rails,Ruby On Rails Plugins,Will Paginate,我正在与will_paginate合作创建一个可排序和可搜索的表。除搜索框过滤器外,我已使所有内容正常工作。现在,当我选择一个不同的查询而不是标题并搜索某个内容时,它只搜索标题查询。有人能帮我指出哪里出了问题吗 谢谢大家! 索引控制器: filter = case params['sform'] when "filter=Title" then "title" when "filter=Size" then "size" when "filter=Film Type" then "f
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
索引视图:
<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>
仍然无法确定如何按筛选器类型进行搜索。还有什么建议吗?在最后一部分:
conditions = ["title LIKE ?", "%#{params[:query]}%"] unless params[:query].nil?
您显式传递的条件是按标题搜索的。这就是您要做的吗?也可以尝试将您的值设置为变量。values={:filter=filter}condition=:filter LIKE?,params[:query]嗯,我不确定我是否知道你所说的设置我的值是什么意思。我从另一个网站得到了这个代码。。。我对ruby还不是很好。。。我可以四处走动,了解其中的大部分内容,但这让我感到困惑。如果过滤器是大小,那么我想根据大小等进行查询。