问题
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