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.
F7aaedb7942594057563a5c48892c8b0?d=identicon&s=25 Alex Sörensen (alexsoerensen)
on 2006-04-29 09: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 %>
5b8e28dfc732a50566c077f212871ff6?d=identicon&s=25 Liang Gao (lgao)
on 2006-04-29 10: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.
F7aaedb7942594057563a5c48892c8b0?d=identicon&s=25 Alex Sörensen (alexsoerensen)
on 2006-04-29 11: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.
C00236792d730f1f9dfe8443d38d9492?d=identicon&s=25 Mike Berrow (mberrow)
on 2006-04-30 05: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 Berrow
350b7c39d2973b5ae483c65729a5bfcc?d=identicon&s=25 Morten (Guest)
on 2006-05-03 19: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
85d195de311934aa4b9cbf073089260a?d=identicon&s=25 Nathan Hesson (Guest)
on 2006-05-03 19:08
(Received via mailing list)
On 4/29/06, Alex Sörensen <as@hippolime.com> 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
9ea8229b9b6503cde77171ffcfe2eedc?d=identicon&s=25 Sonny Chee (soroe)
on 2006-05-07 01: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 <as@hippolime.com> 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
4e6e46de38a6d9161af21c32373a09df?d=identicon&s=25 Guest (Guest)
on 2006-12-13 04:49
Does this actually work ? It's definitely not working on my application.


Sonny Chee 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
9fdc4c40cd7917091e0ad126c2a7d86b?d=identicon&s=25 Kiran Soumya (kiran1234)
on 2006-12-13 05: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.
3e6bbd5a01437feec48adf3838b15d91?d=identicon&s=25 askegg (Guest)
on 2006-12-13 05: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>
Fbc469af03fa730a761a47bde5fdd2cb?d=identicon&s=25 Dave Cornelius (davecorn)
on 2007-06-06 15: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.