I'm having a problem with a submit event
What I'm trying to do is to update a record using radio inputs... and
everything is ok, but when I do a fast double-click on the radio
button, the form duplicates and I'm not sure if its a javascript
problem or a rails problem cause its rendering the partial again
(which shouldn't be happening)

Somebody had this problem before or know how to fix it?

Thanks

Javier

--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Colin Law at Jan 5, 2012 at 4:45 pm

    On 5 January 2012 16:33, JavierQQ wrote:
    I'm having a problem with a submit event
    What I'm trying to do is to update a record using radio inputs... and
    everything is ok, but when I do a fast double-click on the radio
    button, the form duplicates and I'm not sure if its a javascript
    problem or a rails problem cause its rendering the partial again
    (which shouldn't be happening)
    Look in the rails log to see what is happening (development.log
    assuming in development mode).
    Once you understand what is happening you will be better placed to fix it.

    Colin

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Javier Quarite at Jan 5, 2012 at 4:55 pm

    On Thu, Jan 5, 2012 at 11:44 AM, Colin Law wrote:
    Look in the rails log to see what is happening (development.log
    assuming in development mode).
    Once you understand what is happening you will be better placed to fix it.

    Colin
    Yes, I've done that and every time I click on that radio... it makes an
    update and it saves the chosen option, if I make 2 fast clicks (or N
    clicks) it renders 2 (or N ) partials.

    In my log after doing the update in database, it renders the partial and
    after that it calls the update.js that I use, is there a way to avoid this
    behavior? or it something related to the javascript?

    Thanks

    Javier

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Brynjolfur Thorvardsson at Jan 6, 2012 at 7:01 am
    Hi, this sounds like a javascript problem. Javascript is executed asynchroniously on your browser so clicking fast means that you are sending more than one submit from the same form, causing Rails to return more than once from the controller. There are probably many ways to avoid this but one is to put in a delay of say half a second in your javascript submit code.

    Fra: rubyonrails-talk@googlegroups.com På vegne af Javier Quarite
    Sendt: 5. januar 2012 17:55
    Til: rubyonrails-talk@googlegroups.com
    Emne: Re: [Rails] Duplicate forms after click


    On Thu, Jan 5, 2012 at 11:44 AM, Colin Law wrote:

    Look in the rails log to see what is happening (development.log
    assuming in development mode).
    Once you understand what is happening you will be better placed to fix it.

    Colin


    Yes, I've done that and every time I click on that radio... it makes an update and it saves the chosen option, if I make 2 fast clicks (or N clicks) it renders 2 (or N ) partials.

    In my log after doing the update in database, it renders the partial and after that it calls the update.js that I use, is there a way to avoid this behavior? or it something related to the javascript?

    Thanks

    Javier
    --
    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@googlegroups.com .
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com .
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Javier Quarite at Jan 6, 2012 at 11:44 am

    On Fri, Jan 6, 2012 at 2:00 AM, Brynjolfur Thorvardsson wrote:

    Hi, this sounds like a javascript problem. Javascript is executed
    asynchroniously on your browser so clicking fast means that you are sending
    more than one submit from the same form, causing Rails to return more than
    once from the controller. There are probably many ways to avoid this but
    one is to put in a delay of say half a second in your javascript submit
    code. ****

    **
    Yes, that's what I found. I send a lot of ajax request because the function
    that do the submit is this

    function save_response_with_ajax(t){
    $('#edit_response_set_' + t).submit();
    }

    I think that its way to simple and without any kind of validation and the
    radio button is this

    <%= rs_a_form.input :answer_id, :collection =>
    response_set.question.answers.map { |a| [build_choice(a) , a.id] }, :as =>
    :radio, :input_html => {:onclick => 'save_response_with_ajax(' +
    response_set.id.to_s + ');'} %>

    Should I change that submit() into a $.ajax() ?

    Thanks

    Javier

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Brynjolfur Thorvardsson at Jan 6, 2012 at 12:34 pm
    Hi, I'm not really that familiar with rails/javascript integration. You are using a radiobutton to submit a form, and using the onClick event, have you considered simply using an input button instead? My suggestion about using a delay probably wouldn't work, what would you delay and where?

    When you click rapidly on the radiobutton, the onClick events fires each time causing a fresh submit. Rails simply accepts these submits and sends the appropriate pages back. You could have a global variable in your javascript, e.g. "submitted=false" and then set this to true on first submit and check it before each submit. The variable would be set to true again each time the page refreshes, if you need to reset it when a partial is loaded then just include a bit of javascript in that partial that resets the variable.


    Fra: rubyonrails-talk@googlegroups.com På vegne af Javier Quarite
    Sendt: 6. januar 2012 12:44
    Til: rubyonrails-talk@googlegroups.com
    Emne: Re: [Rails] Duplicate forms after click


    On Fri, Jan 6, 2012 at 2:00 AM, Brynjolfur Thorvardsson wrote:
    Hi, this sounds like a javascript problem. Javascript is executed asynchroniously on your browser so clicking fast means that you are sending more than one submit from the same form, causing Rails to return more than once from the controller. There are probably many ways to avoid this but one is to put in a delay of say half a second in your javascript submit code.


    Yes, that's what I found. I send a lot of ajax request because the function that do the submit is this

    function save_response_with_ajax(t){
    $('#edit_response_set_' + t).submit();
    }

    I think that its way to simple and without any kind of validation and the radio button is this

    <%= rs_a_form.input :answer_id, :collection => response_set.question.answers.map { |a| [build_choice(a) , a.id<http://a.id>] }, :as => :radio, :input_html => {:onclick => 'save_response_with_ajax(' + response_set.id.to_s + ');'} %>

    Should I change that submit() into a $.ajax() ?

    Thanks

    Javier
    --
    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@googlegroups.com .
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com .
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Daniel Negri at Jan 6, 2012 at 6:15 pm
    I'm using the very old trick:

    // Prevent double click
    $('form').submit(function(){
    $('input[type=submit]', this).attr('disabled', 'disabled');
    });


    --
    Daniel Negri
    e: danielgomesnegri@gmail.com <danielnegri@gmail.com>
    d: +55 61 8494 1441
    Skype: danielnegri
    Follow me on Twitter <http://twitter.com/#!/danielnegri>



    On Fri, Jan 6, 2012 at 10:34 AM, Brynjolfur Thorvardsson wrote:

    Hi, I’m not really that familiar with rails/javascript integration. You
    are using a radiobutton to submit a form, and using the onClick event, have
    you considered simply using an input button instead? My suggestion about
    using a delay probably wouldn’t work, what would you delay and where? ****

    ** **

    When you click rapidly on the radiobutton, the onClick events fires each
    time causing a fresh submit. Rails simply accepts these submits and sends
    the appropriate pages back. You could have a global variable in your
    javascript, e.g. “submitted=false” and then set this to true on first
    submit and check it before each submit. The variable would be set to true
    again each time the page refreshes, if you need to reset it when a partial
    is loaded then just include a bit of javascript in that partial that resets
    the variable.****

    ** **

    ** **

    *Fra:* rubyonrails-talk@googlegroups.com [mailto:
    rubyonrails-talk@googlegroups.com] *På vegne af *Javier Quarite
    *Sendt:* 6. januar 2012 12:44

    *Til:* rubyonrails-talk@googlegroups.com
    *Emne:* Re: [Rails] Duplicate forms after click****

    ** **

    ** **

    On Fri, Jan 6, 2012 at 2:00 AM, Brynjolfur Thorvardsson wrote:****

    Hi, this sounds like a javascript problem. Javascript is executed
    asynchroniously on your browser so clicking fast means that you are sending
    more than one submit from the same form, causing Rails to return more than
    once from the controller. There are probably many ways to avoid this but
    one is to put in a delay of say half a second in your javascript submit
    code. ****

    ****

    ** **

    Yes, that's what I found. I send a lot of ajax request because the
    function that do the submit is this****

    ** **

    function save_response_with_ajax(t){****

    $('#edit_response_set_' + t).submit();****

    }****

    ** **

    I think that its way to simple and without any kind of validation and the
    radio button is this****

    ** **

    <%= rs_a_form.input :answer_id, :collection =>
    response_set.question.answers.map { |a| [build_choice(a) , a.id] }, :as
    => :radio, :input_html => {:onclick => 'save_response_with_ajax(' +
    response_set.id.to_s + ');'} %>****

    ** **

    Should I change that submit() into a $.ajax() ?****

    ** **

    Thanks ****

    ** **

    Javier ****

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.****

    ****

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Javier Quarite at Jan 6, 2012 at 6:31 pm

    On Fri, Jan 6, 2012 at 8:43 AM, Daniel Negri wrote:

    I'm using the very old trick:

    // Prevent double click

    $('form').submit(function(){

    $('input[type=submit]', this).attr('disabled', 'disabled');

    });
    That would work if I had a submit button =)

    The scenario is a ... Exam Question, wich has 4 alternatives. After click
    on an alternative it automatically saves/updates(only happens if its
    already checked)

    And now when I want to do this

    function submit_with_ajax(t){
    $("#myForm").submit(function(){
    alert("hi");
    });
    }

    nothing happens. I'm trying to change that alert with an $.ajax() method in
    order to do something after complete

    Thanks

    Javier

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Hassan Schroeder at Jan 6, 2012 at 7:26 pm

    On Fri, Jan 6, 2012 at 10:31 AM, Javier Quarite wrote:

    The scenario is a ... Exam Question, wich has 4 alternatives. After click on
    an alternative it automatically saves/updates(only happens if its already
    checked)
    If you just want to save the state of that radio button, why are you
    returning anything to the browser at all?

    And even if you wanted to refresh the page content from that partial,
    if it's rendering twice you have a targeting problem in your JS, like
    you're adding the form to the DOM instead of replacing the original,
    or something.

    Regardless, the right thing should happen even with multiple clicks
    in a short time period (so trying to prevent that is pointless, IMO).

    Can you come up with a *simple* test case that demonstrates this
    problem?
    --
    Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
    http://about.me/hassanschroeder
    twitter: @hassan

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Javier Quarite at Jan 6, 2012 at 7:45 pm

    On Fri, Jan 6, 2012 at 2:26 PM, Hassan Schroeder wrote:
    On Fri, Jan 6, 2012 at 10:31 AM, Javier Quarite wrote:

    The scenario is a ... Exam Question, wich has 4 alternatives. After click on
    an alternative it automatically saves/updates(only happens if its already
    checked)
    If you just want to save the state of that radio button, why are you
    returning anything to the browser at all?

    I'm not just only saving the state in order to make the submit after I
    finish the entire exam.
    When I click on letter B (for example) in my Answer table that question got
    "B"
    I have to return this because they may change their answers ( and that
    might be my problem and I'm really considering hide that div)

    And even if you wanted to refresh the page content from that partial,
    if it's rendering twice you have a targeting problem in your JS, like
    you're adding the form to the DOM instead of replacing the original,
    or something.

    Regardless, the right thing should happen even with multiple clicks
    in a short time period (so trying to prevent that is pointless, IMO).

    Can you come up with a *simple* test case that demonstrates this
    problem?
    --
    I'm not sure what you mean by simple test :) but lets say someone just
    click that radio 2 times quickly (not that he/she meant to do that)
    and suddendly it appears another set of alternatives.

    As you mention, try to prevent multiple requests may be not the problem (or
    maybe not here) and I just tried to do this

    $('#edit_response_set_'+t).submit();
    $("#edit_response_set_"+t).slideUp(300).fadeIn(400);

    It's just hiding and showing the form... and I think is the easy solution I
    can get

    Anyway, thanks for your responses... hope you understood what I tried to do
    :)

    Javier

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Christian Bautista at Jan 6, 2012 at 11:55 am
    pretty sure that its not ruby on rails problem. its maybe because of
    your javascript or maybe try to read your codes again
    On Jan 6, 3:00 pm, Brynjolfur Thorvardsson wrote:
    Hi, this sounds like a javascript problem. Javascript is executed asynchroniously on your browser so clicking fast means that you are sending more than one submit from the same form, causing Rails to return more than once from the controller. There are probably many ways to avoid this but one is to put in a delay of say half a second in your javascript submit code.

    Fra: rubyonrails-talk@googlegroups.com På vegne af Javier Quarite
    Sendt: 5. januar 2012 17:55
    Til: rubyonrails-talk@googlegroups.com
    Emne: Re: [Rails] Duplicate forms after click

    On Thu, Jan 5, 2012 at 11:44 AM, Colin Law wrote:

    Look in the rails log to see what is happening (development.log
    assuming in development mode).
    Once you understand what is happening you will be better placed to fix it.

    Colin

    Yes, I've done that and every time I click on that radio... it makes an update and it saves the chosen option, if I make 2 fast clicks (or N clicks) it renders 2 (or N ) partials.

    In my log after doing the update in database, it renders the partial and after that it calls the update.js that I use, is there a way to avoid this behavior? or it something related to the javascript?

    Thanks

    Javier
    --
    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@googlegroups.com .
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com<mailto:rubyonrails-talk+unsub scribe@googlegroups.com>.
    For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Javier Quarite at Jan 6, 2012 at 12:34 pm
    Is there a way to control multiple ajax requests?, I've tried google but as
    far as I get there's a stop() but it's not what I need in my case.

    I hope to find the solution and post it here then


    Javier

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 5, '12 at 4:33p
activeJan 6, '12 at 7:45p
posts12
users6
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase