Reputation: 21
I have a view that contains a series of select tags. I wanted to get the values of the selected values on all the select tags from the view to my controller.
Please see code below:
<% dialog_tag :id => "imonggo_xero_dialog" do %>
<h3><%= @title %></h3>
<h5>Accounts Mapping</h5>
<hr>
<center>
<table id="listing">
<tr>
<th>Imonggo</th>
<th>Xero Account</th>
</tr>
<tr class="<%= cycle "odd", "even" %>">
<td>Total Sales</td>
<td><%= select_tag 'sales', options_for_select(@revenues) %></td>
</t>
<tr class="<%= cycle "odd", "even" %>">
<td>Cash</td>
<td><%= select_tag :cash, options_for_select(@current_accounts)%></td>
</t>
<tr class="<%= cycle "odd", "even" %>">
<td>Credit Card / EFTPOS</td>
<td><%= select_tag :ccard, options_for_select(@current_accounts)%</td>
</table>
</center>
<br>
<p class="indent_top">
<%= button_to 'Save', "/#{@locale}/save_settings"%>
</p>
<% end %>
I want that after clicking the "Save" button, I will pass as parameters the values of the selected items on all the select tags in my view.
Upvotes: 1
Views: 1522
Reputation: 15089
When you submit your form, the selected values in every select tag inside your form is passed in the form of a params
array to the controller to be manipulated like this:
params[:sales]
params[:cash]
params[:ccard]
Upvotes: 0
Reputation: 10673
The key to your problem is properly specifying the controller and action that handles your data in the form_tag
. The following is a code sample that passes two values to a controller via the params hash.
I think that some problems you might have encountered will be down to your use of a button_to
tag. Note that I am using a submit_tag
to handle the form.
<%= form_tag "/my_controller/my_method" do %>
<div>
<%= label_tag "Foo" %>
</div>
<div>
<%= select_tag("foo", options_for_select(@foos_list, :selected => @selected_foo)) %>
</div>
<div>
<%= label_tag "Bar" %>
</div>
<div>
<%= select_tag("bar", options_for_select(@bars_list, :selected => @selected_bar)) %>
</div>
<div>
<%= submit_tag "Save", :name => 'save' %>
<%= submit_tag "Defaults", :name => 'defaults' %>
</div>
<% end %>
----- my_controller -----
...
def my_method
# if statement to distinguish between a save attempt and
# resetting the form to default values
if params[:save]
puts params[:foo]
puts params[:bar]
end
end
...
Upvotes: 1