# Ajax request and redirection

**URL:** <https://www.ruby-forum.com/t/ajax-request-and-redirection/171548>\
**Category:** Rails\
**Created:** [July 4, 2009, 11:55pm UTC](https://www.ruby-forum.com/t/ajax-request-and-redirection/171548 "2009-07-04T23:55:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [July 4, 2009, 11:55pm UTC](https://www.ruby-forum.com/t/ajax-request-and-redirection/171548/1 "2009-07-04T23:55:21Z")

</div>

This is most likely my lack of understanding of Ajax concepts, but I  
cannot find an answer on the web for the following question:

“Should an ajax request triggered by a specific page must come back to  
the same page?”

Here is my jQuery handler in the jsTree that communicates with my server  
side Rails code:

Note that the base\_url is defined at the top of this file as shown  
below:

var base\_url = “/folders/”;  
…  
var handle\_selected = function(docs\_count\_in\_json) {  
hide\_progress();  
//alert(docs\_count\_in\_json.documents\_count);  
var c = docs\_count\_in\_json.documents\_count;  
if (c \> 0) {  
// alert(“Has documents to display.”)  
// should directly go to the documents tab if there are  
documents  
var s\_id =  
jQuery.tree\_reference(‘folders\_tree’).selected.attr(‘id’);  
jQuery.ajax({  
type: “POST”,  
url: base\_url + ‘set\_current\_folder/’ + s\_id,  
beforeSend: show\_progress,  
data: ({authenticity\_token: AUTH\_TOKEN}),  
dataType: “json”,  
error: handle\_ajax\_error,  
success: hide\_progress  
});  
window.location = ‘/documents/’  
return false;  
} else {  
alert(“This folder does not have any documents.”)  
}  
}

So I am POSTing to the url  
[http://localhost:3000/folders/set\_current\_folder/1](http://localhost:3000/folders/set_current_folder/1)

Which invokes the set\_current\_folder method with a params[:id] value for  
my currently selected folder. The set\_current\_folder method is as  
follows:

def set\_current\_folder  
@folder = Folder.find(params[:id])  
session[:nested\_folder\_id] = nil  
unless @folder.practice\_id.blank?  
session[:folder\_id] = @folder.id  
flash[:notice] = “Current folder set to #{current\_folder.name}”  
redirect\_to documents\_path  
else # this should not happen, yet just in case  
flash[:warning] = “Please associate this folder with corresponding  
practice first.”  
redirect\_to(:action =\> ‘edit’, :id =\> session[:folder\_id])  
end  
end

As can be seen above, set\_current\_folder method sets the value of  
currently selected folder in the session in the line of code

```
  session[:folder_id] = @folder.id

```

And the redirects to the documents\_controller’s default index action  
which shows the documents for that folder. I have run the program  
through debugger and it actually hits that line of code and populates  
the :folder\_id variable in the session hash.

But after redirect, it disappears and cannot be seen by the  
documents\_controller. Finally, I get a “server error” on the page where  
I trigger this request instead of ending up on the index.html.erb page  
for the documents\_controller. The server side log shows no errors. On  
the client side, Firefox does not show any error.

Please help me understand what is going on here?

Thanks.

Bharat

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [July 4, 2009, 11:58pm UTC](https://www.ruby-forum.com/t/ajax-request-and-redirection/171548/2 "2009-07-04T23:58:26Z")

</div>

Quick edit:

On the client side, Firefox does not show any error.

I meant to say:

On the client side, Firebug in Firefox does not show any error.
