Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Multiple submit buttons on the same form.


  • Please log in to reply
3 replies to this topic

#1 Byasha

Byasha

    Passenger

  • Members
  • 1 posts

Posted 21 October 2013 - 03:54 PM

Please advise. How can I have multiple submit_tag buttons on the same form? For now I got it working only for one button.

Not sure how to get form_tag to handle mupliple paths. 

 

*Routes.rb*

  resources :actions do
  end
  root 'home#start'
  match '/home/add', to: 'home#add', via: 'get'
  match '/home/subtract', to: 'home#subtract', via: 'get'

  match '/home/multiply', to: 'home#multiply', via: 'get' 

 

*Start.html.erb*

<h1>Calculation page</h1>
<%= form_tag "/home/add",:method => "get" do %>
<p></p>
<p>
<%= label_tag :entered, "Please enter value:" %> </br>
<%= text_field_tag :entered %>
</p>
<p></p>
<p>
<%= label_tag :entered2, "Please enter value:" %> </br>
<%= text_field_tag :entered2 %>
</p>
<%= submit_tag "add", :controller => "home", :action => "add" %>
<%= submit_tag "subtract",  :controller => "home", :action => "subtract" %>
<%= submit_tag "multiply", :controller => "home", :action => "multiply"%>
<% end %>
 
Please advise.
Thank you in advance.


#2 rayray

rayray

    Signalman

  • Members
  • 15 posts

Posted 21 October 2013 - 05:57 PM

I think 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" />
...

And in your controller:

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

  • james likes this

I'm Jack's complete lack of ruby knowledge.


#3 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 22 October 2013 - 12:03 AM

rayray's answer is spot on, but just to expand a little. It is not the submit button that defines the action the form is sent to but rather the form tag that defines the path. You can only have one form on a single web page (including partials) It's not a Rails thing it's an HTML web thing.


Programming is just about problem solving!


#4 Bharat Soni

Bharat Soni

    Dispatcher

  • Members
  • 53 posts
  • LocationAhmedabad

Posted 22 October 2013 - 05:14 AM

 

I think 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" />
...

And in your controller:

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

 

 

 

As rayray's says you can not do it by form_tag because it will take only one path to complete it and you have to write your whole logic in  single method like below with some different strings like for Add "add",  Subtract "subtract"........and so on...

 

But another one more good way to do it by Ajax in this way you don't need to put whole login in single method.........

 

like below.............

<%= submit_tag "add", :onclick=>'return get_action("add");' %>
<%= submit_tag "subtract", :onclick=>'return get_action("subtract");'%>
<%= submit_tag "multiply", :onclick=>'return get_action("multiply");'%>

And in js you can do like................

 
function get_action(str)
{
var first_val = jQyery("#first_textbox_id").val();
var second_val = jQyery("#second_textbox_id").val();

jQuery.ajax({
                    url:'/home/'+str,
                    data: {'first_val' : first_val,
                        'second_val' : second_val
                        },
                    success: function(){
                        //Write anything you want after complete the method.
                    }
                });
} 

Fun with Ruby on Rails





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users