Rails form with ajax and non-ajax buttons

匿名 (未验证) 提交于 2019-12-03 08:56:10

问题:

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.

回答1:

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...



回答2:

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; }); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!