Multi-button form

It cannot be this difficult, especially in rails. Here is my form code
in a partial for the index action of a controller:

<% form_tag({:controller => controller_name, :action => ‘index’},
{:method => :get, :class => ‘form’}) do %>




<%= label_tag :search, “Search by: " %>
<%= select_tag :search_by, options_for_select(a,
params[:search_by]) %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag " Search”, :name => ‘search_button’ %>




<% if status_array %>


<%= label_tag :filter, “Filter by: " %>
<%= select_tag :filter, options_for_select(status_array,
params[:filter]) %>
<%= submit_tag " Filter”, :name => ‘filter_button’ %>


<% end %>



<% end %>

I am trying to detect in the index action of this controller which
button is clicked. Unfortunately, no matter which button I click, I get
the same parameters and no indication of the button clicked. If I
change the method to post as shown below:

<% form_tag({:controller => controller_name, :action => ‘index’},
{:method => :post, :class => ‘form’}) do %>

then the form posts to the create action! I don’t get it, I am asking
it to execute index action explicitly right? Anyway, when I change to
post method then it is easy to detect which button was clicked.

Please help!

Thanks for your time.

Bharat

Well, I don’t know the answer to your main question.

The reason changing to post goes to the create action is because that
is what resource routes do. Do “rake routes” and notice that the URL
to the index and the create are the same except one is GET and the
other is POST.

It may help to look at the HTML being produced. That may provide some
clues.

HTH

Hello Perry,
Thanks for your time. I managed to fake it :). Actually, it looks even
better and it works too! here is what I did:

<% form_tag({:controller => controller_name, :action => ‘index’},
{:method => :get, :class => ‘form’}) do %>




<%= radio_button_tag(:which_action, “Search”,
search_checked?(params[:which_action])) %>
<%= label_tag(:which_action_Search, "Search by: ") %>
<%= select_tag :search_by, options_for_select(a,
params[:search_by]) %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag “Search”, :name => nil %>




<% if status_array %>


<%= radio_button_tag(:which_action, “Filter”,
filter_checked?(params[:which_action])) %>
<%= label_tag(:which_action_Filter, "Filter by: ") %>
<%= select_tag :filter_by, options_for_select(status_array,
get_filter_by_value(status_array, params[:filter_by])) %>
<%= submit_tag “Filter”, :name => nil %>


<% end %>



<% end %>

You can see that I have used radio_button_tag to manipulate the
interface and this gives me what I want and stay with the get option.
So I am on the last strech of my effort. However, “options_for_select”
is driving me crazy as shown in the snippet of code from above:

        <%= select_tag :filter_by, options_for_select(status_array, 

get_filter_by_value(status_array, params[:filter_by])) %>

This is supposed to give me the selected value corresponding to the
currently selected option in drop-down. status_array is set in the
index action as shown below:

@status_array = ProspectStatus.all.collect {|e| [e.name,e.id]}

So it is a two element array with name and id. It displays fine, works
fine only thing is that the selected option does not stay selected. If
someone can help with that, I will appreciate it.

Thanks again.

Bharat

Following up:

The problem is solved:

Here is the solution:

I had to convert the id from integer to string as shown below:

@status_array = ProspectStatus.all.collect {|e| [e.name,e.id.to_s]}

Bharat