Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement redirect response with an AJAX request in Rails 3?

I have a simple scenario where I want to request a page. The request format is AJAX. If there is some error in the controller/action logic for that request, I want to redirect to an error page. The issue is that a redirect is not a JavaScript response type, so I am not sure whether it will work.

If there are no errors, then I want the page to be updated via the appropriate JavaScript response type.

What is best practice to achieve redirect responses given that the request format is AJAX?

like image 545
deruse Avatar asked Jan 19 '26 23:01

deruse


1 Answers

This blog post enlightened me on what I think is the right way to do this, if your ajax response is ajax; at least, in the unobtrusive javascript paradigm. In essense, the ajax call always returns a standard json package, which can be parsed for information payload or a redirect url.

like image 195
DGM Avatar answered Jan 22 '26 18:01

DGM