3

I have a form_for an @object with two buttons.

While the first button renders the 'show action', the second button renders the same form again. So I'd like the latter to be ajax-handled.

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

Maybe I need a form_for with 'remote: true' so that both the buttons are ajax but then, how would I manage the first button to render the proper 'show view'?

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

Thank you.

Darme
  • 6,984
  • 5
  • 37
  • 52

2 Answers2

2

You could try to hook onto the buttons onClick event, remove the data-remote="true" attribute, submit the form and add data-remote="true" again. I dont know if this is really the best way but it should work.

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

Something like this...

klump
  • 3,259
  • 2
  • 23
  • 26
  • Problem with that : if the submit button is actually an `` with a value (commit message), using form.submit() will NOT pass the input commit message of the button that was clicked. – Cyril Duchon-Doris Oct 05 '15 at 16:55
0

I think the easiest approach would be to use the jQuery Form plugin. Then you can just create the form to submit to your non-ajax action, and the ajax functionality will be attached to the submit button itself:

$(".ajax_submit_button").click(function() {
  $(this).closest("form").ajaxSubmit({
    // options go here
    ...
  });
  return false;
});
tsherif
  • 11,502
  • 4
  • 29
  • 27