Rails form with ajax and non-ajax buttons

I have a form_for @object with two buttons.

As long as the first button displays the "show action", the second button displays the same form again. Therefore, I would like the latter to be processed by ajax.

Is it possible to have a non-ajax button and ajax button in the same form, or do I need to change the strategy?

Maybe I need a form_for with 'remote: true' so that both buttons are ajax, but then how could I control the first button to display the correct “show view”?

Or maybe the only real solution is to have two different forms?

Thank.

+3
source share
2 answers

onClick, data-remote="true", data-remote="true" . , , .

function sendWithoutAjax() {
  $('my_form_id').removeAttr("data-remote");
  $('my_form_id').submit();
  $('my_form_id').data( "remote", "true" );
}

- ...

+2

, jQuery Form. , ajax :

$(".ajax_submit_button").click(function() {
  $(this).closest("form").ajaxSubmit({
    // options go here
    ...
  });
  return false;
});
0

All Articles