Forum: Ruby on Rails Multiple actions from one form - possible?

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.
Alex S. (Guest)
on 2006-04-29 11:42
Hello *

Obviously the rhtml below does not work, but it should be apparent what
I try to achieve - I would like to give the user the choice to submit a
form either to action1 or action2 but I cannot figure out how?! It would
be great if someone could give some assistance.

Thanks a lot, Alex

<%= start_form_tag :action1 => 'save', :action2 => 'saveandsend', :id =>
@letter %>
	<%= submit_tag 'Save', action1  %>
	<%= submit_tag 'Send', action2 '%>
  <%= render :partial => 'form' %>
<%= end_form_tag %>
Liang G. (Guest)
on 2006-04-29 12:05
you probably have 2 methods like in your controller

def save
end

def saveandsend
end

You may need to define 2 views like save.rhml and saveandsend.rhtml,
instead of  just one view as you stated.

Also, a form can only have one action, it is not a RoR rule, but a HTML
rule.
Alex S. (Guest)
on 2006-04-29 13:48
Hello Liang, hello *

Thanks for the reply. Is there maybe another way and use in the view:

<button name="save">
<button name="send">

and then use

if params[:save]
else if params [:send]
end

in the controller? The problem I encounter here is that the <button....>
is just a funny little button and not a real button (with name....like
the 'Submit' just below) which I would require....I guess this is an
HTML issue. What would you suggest?

Thanks, Alex

liang gao wrote:
> you probably have 2 methods like in your controller
>
> def save
> end
>
> def saveandsend
> end
>
> You may need to define 2 views like save.rhml and saveandsend.rhtml,
> instead of  just one view as you stated.
>
> Also, a form can only have one action, it is not a RoR rule, but a HTML
> rule.
Mike B. (Guest)
on 2006-04-30 07:23
Hello Alex,

I learned how to do this at
http://tr.openmonkey.com/articles/2006/01/16/multi...

And here is a stripped-down version of what I did to
get action variations driven by which button is selected.
Basically, users are able to change the state or remove items in
some table rows (not shown). One column had checkboxes to
indicated which rows are to be affected).


<%= start_form_tag :action => 'update_list' %>
....
    <input id="update_button_Enable Selected" name="update_button[Enable
Selected]" type="hidden" />
    <button name="_update_button"  type="submit" value="Enable Selected"
onclick="">
        Enable Selected
    </button>

      <button name="_update_button" type="submit" value="Disable
Selected" onclick="Form.getInputs(this.form, null,
'update_button')[0].value = 'Disable Selected">
        Disable Selected
    </button>

      <button name="_update_button" type="submit" value="Remove
Selected" onclick="Form.getInputs(this.form, null,
'update_button')[0].value = 'Remove Selected">
        Remove Selected
    </button>
  </div>
....
<%= end_form_tag %>


In the controller ....

  def update_list
  ....
  ....
      case params[:_update_button]
        when "Enable Selected": enablement = true
        when "Disable Selected": enablement = false
        when "Remove Selected":
          # do the remove thing
        else
          flash[:notice] = "That button is not hooked up properly."
      end
  ....
  ....
  end

-- Mike B.
Morten (Guest)
on 2006-05-03 21:08
(Received via mailing list)
Alex Sörensen wrote:
> @letter %>
> 	<%= submit_tag 'Save', action1  %>
> 	<%= submit_tag 'Send', action2 '%>
>   <%= render :partial => 'form' %>
> <%= end_form_tag %>
>

You need to dynamically change the action for the form using JavaScript.
I'd write a helper function or two:

<script>
   function submitActionOne() {
     var act = '<%=url_for(:action => "action_one", :id => @letter )%>';
     $('myform').action = act;
     $('myform').submit();
   }

   function submitActionTwo() {
     var act = '<%=url_for(:action => "action_two", :id => @letter )%>';
     $('myform').action = act;
     $('myform').submit();
   }
</script>

<%= start_form_tag({ }, { :id => "myform" })%>
    <%= button_to_function("Submit One", "submitActionOne()") %>
    <%= button_to_function("Submit Two", "submitActionTwo()") %>
<%= end_form_tag %>


HTH,

Morten
Nathan Hesson (Guest)
on 2006-05-03 21:08
(Received via mailing list)
On 4/29/06, Alex Sörensen <removed_email_address@domain.invalid> wrote:
> if params[:save]
> liang gao wrote:
> >
>
inside your button tags put something like
onclick=$('my_form_id.submit()
This should make the two buttons act as submit buttons
HTH
Nathan
Sonny C. (Guest)
on 2006-05-07 03:36
Alternatively, a simple solution is to name all of the action buttons
the same but set the value attribute to distinguishing values.  So, in
the rhtml file you would have:


<form action="../item/item_manage" method="POST">
<input id="Add" name="submit_action" type="submit" value="Add">
<input id="Delete" name="submit_action" type="submit" value="Delete">
</form>

In the item_controller.rb file then you can access the parameter
submit_action and take the appropriate action

@action=@params['submit_action']

if (@action == 'Add') then
...blah, blah
else
...blah, blah
end

Soroe.

Nathan Hesson wrote:
> On 4/29/06, Alex Sörensen <removed_email_address@domain.invalid> wrote:
>> if params[:save]
>> liang gao wrote:
>> >
>>
> inside your button tags put something like
> onclick=$('my_form_id.submit()
> This should make the two buttons act as submit buttons
> HTH
> Nathan
Guest (Guest)
on 2006-12-13 05:49
Does this actually work ? It's definitely not working on my application.


Sonny C. wrote:

> <form action="../item/item_manage" method="POST">
> <input id="Add" name="submit_action" type="submit" value="Add">
> <input id="Delete" name="submit_action" type="submit" value="Delete">
> </form>
>
> In the item_controller.rb file then you can access the parameter
> submit_action and take the appropriate action
>
> @action=@params['submit_action']
>
> if (@action == 'Add') then
> ...blah, blah
> else
> ...blah, blah
> end
Kiran S. (Guest)
on 2006-12-13 06:51
(Received via mailing list)
it works fine for me.

Can u specify the error u get???

Can u check ur development log once how it passes the parameters??

Also try out assigning like this...

@action = params[:submit_action].strip

Regards.
askegg (Guest)
on 2006-12-13 06:53
(Received via mailing list)
Not that I have tested this, but....

I suspect the "submit_action" values are clobbering each other when
parsed.

Try putting them in two different forms, and I am not sure if that name
will work.

<form action="../item/item_manage" method="POST">
  <input id="Add" name="submit[action]" type="submit" value="Add">
</form>
<form action="../item/item_manage" method="POST">
  <input id="Delete" name="submit[action]" type="submit"
value="Delete">
</form>
Dave C. (Guest)
on 2007-06-06 17:54
I find it easiest to specify unique names for each submit button. You
avoid name collisions this way, plus, the only submit name/value that
gets posted is the one that is clicked.

----
View
----
<%= start_form_tag %>
<%= submit_tag 'Add',   :name => 'add_button' %>
<%= submit_tag 'Delete, :name => 'delete_button' %>
<%= end_form_tag %>

----------
Controller
----------
if !params[:add_button].nil?
  ...
elsif !params[:delete_button].nil?
  ...
else
  ...
end
This topic is locked and can not be replied to.