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