Question

I have a link that fires off an ajax request using Rails UJS. The response actually replaces the parent element containing the link. The ajax:before event fires off as expected, but the ajax:complete event never fires. I thought this was just a result of the element being replaced. But just to check, I bound directly to an ajax "complete" callback without using UJS and it works fine.

So my question is why does the "complete" callback work when binding directly to an ajax call but doesn't work when binding through UJS to the "ajax:complete"?

Was it helpful?

OTHER TIPS

I wasn't thinking when I posted my comment earlier (apologies) but now I think I am awake enough to help you :).

Although JQuery supports the script() function, you don't normally fire UJS through Ajax! Use a regular form_tag with remote:true (which will stick a 'data-remote' tag within your form. Ajax works with JSON and with UJS, you will be returning pure js

Remember that the action will render to a form usually by the name of app/views/<controller>/<action>.js.erb (or whatever template technology you are currently using)

Remember to have the respond_to block in your controller such as

respond_to do |format|
  format.js   #note.. not .json
end

also ensure that your request headers accept javascript.. 'application/javascript', 'script'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top