user1579627
user1579627

Reputation: 25

How can I use an if statement on a select_tag in my model Rails

Okay so when I submit the form these are the parameters passed through my browser:

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

Basically what I need to do is in my model test the first parameter "Search". Ex:

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

I have been pounding my head over this and cant figure it out. My ruby code is below i may cut out some "end"'s and stuff like that to save room.

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 model

      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  

Upvotes: 0

Views: 89

Answers (1)

Shreyas Agarwal
Shreyas Agarwal

Reputation: 1128

I guess you need to pass 2 parameters to your search function present in the model

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

And call search from your controller in this way

Search.search(params[:Search],params[:search])

Upvotes: 1

Related Questions