Reputation: 13
So I am having an issue with an if else statement in the controller. I have 4 radiobuttons on my page and one hidden field. There are also 4 divs where only one can be visible at a time.When a different radiobutton is checked another div is shown. This should work like a 'complex' search interface. In the hidden field I'm inserting a value from 1 to 4 (depending on what radio button is checked). In the controller I'm looking at the value of the hidden field and my functions should change accordingly. My problem is that it does not work. I tried a couple of different things but didn't find an answer to my problem.
Here's my code
<div>
<%= radio_button_tag 'searchRBN', 'patient', true, :onchange => "checkRadioButton()" %>
<%= label_tag :byPatient_patient, "Patient" %>
<%= radio_button_tag 'searchRBN', 'staff', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byStaff_staff, "Staff" %>
<%= radio_button_tag 'searchRBN', 'ocmw', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byOcmw_ocmw, "OCMW" %>
<%= radio_button_tag 'searchRBN', 'mutuality', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byMutuality_mutuality, "Mutuality" %>
</div>
<%= hidden_field_tag(:hidden_one, "1") %>
<div id="searchByPatient">
<%= form_tag patients_path, :method => 'get' do %>
<p>
<%= text_field_tag :search1, params[:search1] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</div>
<div id="searchByStaff" class="notVisible">
<%= form_tag patients_path, :method => 'get' do %>
<%= text_field_tag :search2, params[:search2] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
</div>
def index
@staff_all = Staff.all
@ocmw_all = Ocmw.all
@mutuality_all = Mutuality.all
if params[:hidden_one] == '1'
@patients = Patient.searchByName(params[:search1])
elsif params[:hidden_one] == '2'
@patients = Patient.searchByStaff(params[:search2])
else
@patients = Patient.all
end
end
def self.searchByName(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
def self.searchByStaff(search)
if search
find(:all, :conditions => ['marriedTo LIKE ?', "%#{search}%"])
else
find(:all)
end
end
Mathias A.
Upvotes: 0
Views: 3719
Reputation: 3857
I think your hidden field is outside of your form tag so it will be never submitted to the server.
Another solution would be to simply put a hidden field to each search form you have to identify the corresponding search form on the server.
Example:
<div id="searchByPatient">
<%= form_tag patients_path, :method => 'get' do %>
<%= hidden_field_tag :search_type, :search_by_patient %>
<p>
<%= text_field_tag :search1, params[:search1] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</div>
Upvotes: 1