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 %> -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
Alex Sörensen
2006-Apr-29 09:49 UTC
[Rails] Re: Multiple actions from one form - possible?
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.-- Posted via http://www.ruby-forum.com/.
dblack@wobblini.net
2006-Apr-29 12:17 UTC
[Rails] Multiple actions from one form - possible?
Hi -- On Sat, 29 Apr 2006, Alex Sörensen wrote:> 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 %>I offer this, not as the best possible solution but as the one I came up with when trying to do the same thing: I used one helper tag and one plain input/submit tag, like this: <p><input name="final" type="submit" value="Submit (final version)"/></p> <p><%= submit_tag "Save and finish later" %></p> And then in the controller I checked whether or not params[:final] was set. I couldn''t come up with a more homogenous way, but maybe someone will enlighten us. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" PDF now on sale! http://www.manning.com/black Paper version coming in early May!
Alex S?rensen wrote:> 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 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
2006-Apr-29 15:26 UTC
[Rails] Re: Multiple actions from one form - possible?
On 4/29/06, Alex S?rensen <as@hippolime.com> wrote:> > 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. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >inside your button tags put something like onclick=$(''my_form_id.submit() This should make the two buttons act as submit buttons HTH Nathan -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060429/ad0ec63b/attachment.html
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'' %> .... <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 -- Posted via http://www.ruby-forum.com/.
Sonny Chee
2006-May-06 23:36 UTC
[Rails] Re: Re: Multiple actions from one form - possible?
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-- Posted via http://www.ruby-forum.com/.
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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---