Rails is not passing the “commit” button parameter

前端 未结 5 932
醉梦人生
醉梦人生 2021-02-08 05:27

Reinstalling a Rails app on a new server. Part of the app can fork in one of two directions based on the button the user selects. This part isn\'t working, and when I look at

相关标签:
5条回答
  • 2021-02-08 06:01

    Just add name: "commit", value: "Save"to your form submit button:

    form_for @object do |f|
      ...
      f.button :submit, "Save", name: "commit", value:"Save"
    end
    

    and then you will have params[:commit] equals to "Save" in the controller.

    0 讨论(0)
  • 2021-02-08 06:03

    Had a simular problem with a disable-button-on-submit feature. We solved it by adding a hidden input field with the same name and value before submitting the form.

    function disableButtonAndSubmit()
    {
      var input = $("<input type='hidden' />").attr("name", $(this)[0].name).attr("value", $(this)[0].value);
      $(this).closest('form').append(input);
      $(this).attr('disabled', 'disabled').html('Loading…');
      $(this).closest('form').submit();
    }
    
    $('#somewhere button').click(disableButtonAndSubmit);
    
    0 讨论(0)
  • 2021-02-08 06:03

    Check that your submit input is named commit or it's label will not be sent.

    The resulting html should be:

    <input type="submit" name="commit" label="...>
    
    0 讨论(0)
  • 2021-02-08 06:16

    I ran into this same problem, and the answers here pointed me in the right direction. However, rather than the suggestions to be adding hidden form inputs or giving up on the double submit block, you can simply add a setTimeout function on your double submit block with a timeout of 1 millisecond, which allows the double submit block to work without preventing the submission of the button.

    0 讨论(0)
  • 2021-02-08 06:17

    I looked into something like this awhile ago, where there is inconsistency in how different browsers would pass in the value of a submit button on a form. I found the only practical solution was to have javascript in the button to set a hidden field, and use that value instead.

    Here is some of my code to differentiate between a save and exit, which goes one way, and save and continue which go another:

      <%= hidden_field_tag 'step_commit', '' %>
      <span style="float:left;">
        <%=submit_tag 'Cancel', :name=>'cancel', :onclick=>"javascript:location.href='/';return false;" %>
        <%=submit_tag 'Save and Exit', :name=>'exit', :onclick=>"javascript:$('step_commit').value='exit';" %>
      </span>
      <span style="float:right;">
        <%=submit_tag 'Save and Continue', :name=>'continue', :onclick=>"javascript:$('step_commit').value='continue';" %>
      </span>
    
    0 讨论(0)
提交回复
热议问题