# Multi-button form

**URL:** <https://www.ruby-forum.com/t/multi-button-form/165350>\
**Category:** Rails\
**Created:** [April 12, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/multi-button-form/165350 "2009-04-12T23:24:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [April 12, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/multi-button-form/165350/1 "2009-04-12T23:24:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [April 13, 2009, 2:06am UTC](https://www.ruby-forum.com/t/multi-button-form/165350/2 "2009-04-13T02:06:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [April 13, 2009, 3:19am UTC](https://www.ruby-forum.com/t/multi-button-form/165350/3 "2009-04-13T03:19:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [April 13, 2009, 3:52am UTC](https://www.ruby-forum.com/t/multi-button-form/165350/4 "2009-04-13T03:52:07Z")

</div>

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
