Multiple actions from one form - possible?

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 %>

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.

Hello Liang, hello *

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

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.

Hello Alex,

I learned how to do this at
http://tr.openmonkey.com/articles/2006/01/16/multiple-ajax-submit-buttons-in-a-rails-form

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’ %>



Enable Selected

  <button name="_update_button" type="submit" value="Disable 

Selected" onclick=“Form.getInputs(this.form, null,
‘update_button’)[0].value = 'Disable Selected”>
Disable Selected

  <button name="_update_button" type="submit" value="Remove 

Selected" onclick=“Form.getInputs(this.form, null,
‘update_button’)[0].value = 'Remove Selected”>
Remove Selected

.... <%= 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.

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:

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

HTH,

Morten

On 4/29/06, Alex Sörensen [email protected] 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

Does this actually work ? It’s definitely not working on my application.

Sonny C. wrote:

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

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:

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 [email protected] 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

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.

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

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.