Re: Bug: Multiple Submit Buttons w/ form_remote_tag

5 views
Skip to first unread message

Eddie Roadcap

unread,
Sep 21, 2006, 6:21:51 PM9/21/06
to rubyonra...@googlegroups.com
The following code placed in application_helper.rb or loaded via /lib
works around this problem while allowing normal submit_tag() useage.
This is not well tested, but seems to do the trick for me.

class ActionView::Base
alias_method :rails_submit_tag, :submit_tag
def submit_tag(value = "Save changes", options = {})
options[:id] = (options[:id] || options[:name] || :commit)
options.update(:onclick => "$('#{options[:id]}').value =
'#{value}'")
rails_submit_tag(value, options)
end
end

So this rails code:
<%= submit_tag 'Transfer', :name => :submit %>
<%= submit_tag 'Check IN' %>
produces this HTML:
<input id="submit" name="submit" onclick="$('submit').value =
'Transfer'" value="Transfer" type="submit">
<input id="commit" name="commit" onclick="$('commit').value = 'Check
IN'" value="Check IN" type="submit">

derek.haynes wrote:
> I've run across a problem when attempting to submit a form with the
> form_remote_tag when the form contains multiple submit buttons.
>
> My form contains 2 submit buttons, a "Preview" button and a "Post
> Message" button:
> <%= submit_tag 'Preview' %>
> <%= submit_tag 'Post Message' %>
>
> When the form is submitted, the value of @params['commit'] is the
> value of the first submit tag ('Preview' in this case) no matter what
> button is used to submit the form.
>
> This is not present when changing the form to a standard non-ajax form.
>
> Any one else run across this problem or have a suggested fix?
>
>
> --
> Derek Haynes
> HighGroove Studios - http://www.highgroove.com
> Atlanta, GA
> Keeping it Simple.
> 404.593.4879

--
Posted via http://www.ruby-forum.com/.

Mike Garey

unread,
Sep 21, 2006, 6:35:01 PM9/21/06
to rubyonra...@googlegroups.com
what's the status on this bug? Is it something that's ever going to be
fixed? I try to stay away from Ajax forms with multiple buttons
because of this.

Mike

_Kevin

unread,
Sep 21, 2006, 8:52:34 PM9/21/06
to Ruby on Rails: Talk

With normal forms, if you give the submit button a different name
attribute, the submitted parameters will contain an entry for it.

<%= submit_tag 'Preview', :name=>'preview' %>

params['preview'] is then defined on submission, and you can use that
to figure out which button was pressed. No idea if this works for AJAX
forms, but it seems like it should.

_Kevin

Daniel Morris

unread,
Sep 29, 2006, 7:09:49 AM9/29/06
to rubyonra...@googlegroups.com
I've tried a simple method of creating a hidden 'submit_id' field.

Every submit button updates it with ':onclick'.

The code I use:

class ActionView::Base


def submit_tag(value = "Save changes", options = {})

options.update(:onclick => "$('submit_id').value = '#{value}'")
super(value,options)
end
end

which is placed in application_helper.rb.

And on your form place a hidden field like so (for example):

<%= %Q!<input id="submit_id" name="submit_id" type="hidden"
value="12345">! %>

The :submit_id param will contain the right value. At least this is what
worked for me !!

Cheers

Daniel Morris

unread,
Sep 29, 2006, 7:41:51 AM9/29/06
to rubyonra...@googlegroups.com
This is a more compact solution- it only needs to be inserted into your
application_helper.rb

class ActionView::Base
attr_accessor :got_submit


def submit_tag(value = "Save changes", options = {})
options.update(:onclick => "$('submit_id').value = '#{value}'")

if options[:name] && options[:name]==:commit
options[:name]="commit_sub"
elsif options[:name].nil?
options[:name]="commit_sub"
end
res=super(value,options)
if @got_submit.nil?
@got_submit=true
res = res << %Q!<input id="submit_id" name="commit" type="hidden"
value="12345">!
end
res
end
end


Question:

Is there a better way of inserting the hidden field into a form?

My premise in doing it the way I have done is that you only want a
hidden 'submit_id' field when you actually have submit buttons.

David Lake

unread,
Nov 2, 2006, 9:23:38 AM11/2/06
to rubyonra...@googlegroups.com
Hi,

If you have only got 2 buttons then you can take advantage of the fact
that disabling a button means that it won't get posted. In the code
below, when the user presses 'Save', the save_button gets disabled and
the controller sees the presence of preview_button. If 'Preview' gets
pressed then the first button ('save_button') gets seen as described in
the earlier posts. It's ugly and the controller logic looks upside down,
but it seems to work for me...

<%= submit_tag "Save", :name => 'save_button', :disable_with => "..."
%>
<%= submit_tag "Preview", :name => 'preview_button' %>

Controller:

def save
# Save button gets disabled when pressed, so no save button means
preview...
if !@params['save_button'].nil? then
do_preview
else
do_save
end
end

derek.haynes wrote:
> I've run across a problem when attempting to submit a form with the
> form_remote_tag when the form contains multiple submit buttons.
>
> My form contains 2 submit buttons, a "Preview" button and a "Post
> Message" button:
> <%= submit_tag 'Preview' %>
> <%= submit_tag 'Post Message' %>
>
> When the form is submitted, the value of @params['commit'] is the
> value of the first submit tag ('Preview' in this case) no matter what
> button is used to submit the form.
>
> This is not present when changing the form to a standard non-ajax form.
>
> Any one else run across this problem or have a suggested fix?
>
>
> --
> Derek Haynes
> HighGroove Studios - http://www.highgroove.com
> Atlanta, GA
> Keeping it Simple.
> 404.593.4879

Reply all
Reply to author
Forward
0 new messages