Forum: Ruby on Rails Multi-button form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bharat R. (Guest)
on 2009-04-13 01:24
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 %>
    <div class="columns">
      <div class="column left">
        <p>
          <%= 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' %>
        </p>
      </div>
      <div class="column right">
        <% if status_array %>
          <p>
            <%= label_tag :filter, "Filter by: " %>
            <%= select_tag :filter, options_for_select(status_array,
params[:filter]) %>
            <%= submit_tag " Filter", :name => 'filter_button' %>
          </p>
        <% end %>
      </div>
    </div>
    <div class="clear"></div>
  <% 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
Perry S. (Guest)
on 2009-04-13 04:06
(Received via mailing list)
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
Bharat R. (Guest)
on 2009-04-13 05:19
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 %>
    <div class="columns">
      <div class="column left">
        <p>
          <%= 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 %>
        </p>
      </div>
      <div class="column right">
        <% if status_array %>
          <p>
            <%= 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 %>
          </p>
        <% end %>
      </div>
    </div>
    <div class="clear"></div>
  <% 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
Bharat R. (Guest)
on 2009-04-13 05:52
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
This topic is locked and can not be replied to.