0

好的,所以当我提交表单时,这些是通过我的浏览器传递的参数:

{"Search"=>"Job Letter and CD #",
"utf8"=>"✓",
"search"=>"g",
"commit"=>"Search"}

基本上我需要做的是在我的模型测试中第一个参数“搜索”。前任:

if Search == "Job Letter and CD #"
..do stuff..
elsif Search == "something else"
..do different stuff..
else
..do something..

我一直在敲我的头,无法弄清楚。我的红宝石代码在下面,我可能会删掉一些“结束”之类的东西以节省空间。

index.html.erb

<form class="well form-search right">
<%= select_tag "Search", options_for_select([ "Job Letter and CD #", "Date", "Cust",            "Job", "Date shipped", "Date billed", "Billed by" ], params[:search]) %>
<%= form_tag reports_path, :method => 'get' do %> 
<%= text_field_tag :search, params[:search], :class=> "form-search", :align => "right" %>
<%= submit_tag "Search", [ :JOBLETTER_CD_NUMBER, :DATE, :CUST, :JOB, :DATE_SHIPPED, :DATE_BILLED, :billed_by ] => nil, :class => "btn btn-success", :align => "right" %>

report.rb 模型

      def self.search(search)  
if Search == 'Job Letter and CD #'
  where('JOBLETTER_CD_NUMBER LIKE ?', "%#{search}%") 
elsif Search == 'Date'
  where('DATE ?', "%#{search}%")
else
  scoped  
end
end  
4

1 回答 1

1

我想您需要将 2 个参数传递给模型中存在的搜索功能

def self.search(what_to_search,search_string)
  if what_to_search == 'Job Letter and CD #'
     where('JOBLETTER_CD_NUMBER LIKE ?', "%#{search_string}%") 
  elsif what_to_search == 'Date'
     where('DATE ?', "%#{search_string}%")
  else
      scoped  
  end
end

并以这种方式从您的控制器调用搜索

Search.search(params[:Search],params[:search])
于 2012-08-09T14:59:03.270 回答