How to submit my remote update form via AJAX?

纵然是瞬间 提交于 2019-12-12 03:42:16

问题


I have a form that will edit the record, I want it to use ajax though when submitting,

= simple_form_for(@lesson, :html => {"data-type" => :html, :id=>'lesson_form'}, :remote=>true) do |lesson_form|

= lesson_form.button :submit, 'Update'

but, in the lessons controller at update action, the request.xhr? evaluates 0, although I am setting remote to true at the form options above; request.xhr? does not evaluate to true.

So, what am I missing here ? How to use ajax when update my record ? Isn't setting remote to true enough ? Any ideas please ?

thanks


回答1:


You can do it many ways. Some of them are:

If passing form data using GET, you can use jquery's ajax() and send the request. That way you can use an explicit ajax trick i.e, have in-frame as medium of communication b/w form and submit. You can use this to validate the form as well, for example: checking to see if the username exists or not. In that scenerio you will have javascript interacting with your iframe and that iframe will have certain rules or markups for username that exists and vice versa.

Hope this helps. I am happy to provide you with some sample code i wrote a looooong time ago if you want.



来源:https://stackoverflow.com/questions/11477359/how-to-submit-my-remote-update-form-via-ajax

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