Monday, June 11, 2007

Using Multiple Buttons in a Form

Sometimes, one button to rule them all just isn't good enough, and you need more than one button in a form. With Rails, it's surprisingly easy to set up multiple buttons on a form.

In the View, we'll have the form with three buttons: Button 1, Button 2 and Button 3


...Juicy Form Goodness...

<%= submit_tag 'Button 1', :name => "submit" %>
<%= submit_tag 'Button 2', :name => "submit" %>
<%= submit_tag 'Button 3', :name => "submit" %>


In the Controller, we'll use a case statement to decipher which button was pressed and redirect to the corresponding action.

def filter_reports
case params[:submit]
when "Button 1"
redirect_to :action => :button_1_action, :id => params[:id]
when "Button 2"
redirect_to :action => :button_2_action, :id => params[:id]
when "Button 3"
redirect_to :action => :button_3_action, :id => params[:id]
end
end

Awesome, now you can add as many buttons as you want/need to a form.

No comments: