Problem preventing double click with ajax submit button

For a regular form I use the :onlick option to disable the submit button
after the first click in order to prevent double clicks (leading to
double entries):

<% form_tag :action => ‘create’ do %>
<%= submit_tag “Create”, :onclick =>
“this.disabled=true,this.form.submit();”%>

I’m having trouble using this same method to prevent double clicks in an
ajax form:

<% form_remote_tag :url => {:action => ‘enter_payment’} do %>
<%= submit_tag ‘Enter’, :onclick =>
“this.disabled=true,this.form.submit();” %>

This controller method ‘enter_payment’ for this ajax form creates an
object and re-renders a couple partials. With this code, the object is
created, but the following error comes up when it tries to render the
partials:

“Can only render or redirect once per action”

When I comment one of the partials out, it redirects to the other
partial instead of re-rendering it within the original page. How might I
be able to fix this?

Thanks,

Peter

how about posting the controller code, where the error appears?

my guess would be, that

return if request.xhr?

prevents the trouble for non-ajax by simply returning and never reaching
the render calls

make something like logger.info(“foo”) to see, if it reaches the lines
at all
anyway, the error message is right, since two render calls are not
allowed

and if the partials do not contain rjs, that will fail and render the
whole partial as a page. one reason, there’s nowhere any information,
which html-tag to replace or use as target

one option would be:

<% form_remote_tag :url => {:action => ‘enter_payment’}, :update =>
‘my_html_id’ do %>

not exactly, what i would prefer, which would be more like

render :update do |page|
page.replace “my_html_id_1”, :partial => “events”
page.replace “my_html_id_2”, :partial => “inputs”
end

in this case, without changes to your view code

Here’s the controller code. It works fine without the onclick option in
the submit button (renders the partials without reloading the page). I’m
guessing the submit method I’m using in the :onclick option is expecting
a redirect. That would explain it redirecting me to a partial and it
failing when it gets multiple redirect commands. I’m pretty javascript
retarded though. I just hack what I see and hope it works :confused:

def input
@person = Person.find(params[:id])
name = (params[:name])
if @person.update_attributes(params[:person])
event = Event.create(:person_id => @person.id, :name => name,
:date => Time.now)
@new_event = event
return if request.xhr?
render :partial => ‘events’
render :partial => ‘inputs’
end
end

Thanks again for having a look at this Thorsten.

Thanks for the ideas. I get a different, but still incorrect result when
I submit to a method with inline rjs like you’re suggesting. It
redirects the browser to the controller method but, instead of rendering
the partial, it renders code for the javascript instructions.

Peter M. wrote:

Thanks for the ideas. I get a different, but still incorrect result when
I submit to a method with inline rjs like you’re suggesting. It
redirects the browser to the controller method but, instead of rendering
the partial, it renders code for the javascript instructions.

that one… :slight_smile:

somehow it’s loosing the information, that’s an ajax call and handles
the returned text as html

i had some trouble with remote forms and manually calling submit, maybe
you can hack around it with calling onsubmit instead

or test it with link_to_remote, until you get working code, without
additional trouble of form-submit

and remember, not to use :update =>, but your code:
<% form_remote_tag :url => {:action => ‘enter_payment’} do %>
in the view

Thorsten M. wrote:

that one… :slight_smile:

somehow it’s loosing the information, that’s an ajax call and handles
the returned text as html

i had some trouble with remote forms and manually calling submit, maybe
you can hack around it with calling onsubmit instead

or test it with link_to_remote, until you get working code, without
additional trouble of form-submit

and remember, not to use :update =>, but your code:
<% form_remote_tag :url => {:action => ‘enter_payment’} do %>
in the view

Well I’m glad I’m not the only one having trouble with this :slight_smile: Thanks
again Thorsten.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs