Rails : Handling Multiple submit buttons

You can’t submit to multiple paths. Just submit everything to the same action:

<%= submit_tag "add" %>
<%= submit_tag "subtract" %>
<%= submit_tag "multiply" %>

This gives you the following html:

<input type="submit" value="add" name="commit" />
<input type="submit" value="subtract" name="commit" />
<input type="submit" value="multiply" name="commit" />

And in your controller:

if params[:commit] == "add"
  # addition
elsif params[:commit] == "subtract"
  # subtraction
elsif params[:commit] == "multiply"
  # multiplication
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s