[jQuery] Ajax with jQuery and Rails

[jQuery] Ajax with jQuery and Rails


I am learning Ajax with jQuery. My server is Ruby on Rails.
In a nutshell, I do not know how to let jQuery give up control and let
the server side take over. I have a jsTree displaying various nodes
(folders in my application). If a clicked folder has documents in it,
then I want to let the Ruby on Rails server code redirect the control
to a different page that displays a list of documents in belonging to
that folder. I can see that happening in the server side log, but the
control stays on the folders tree page.
Here is the jQuery snippet that makes the call to the server method
that does the redirecting:
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
});
} else {
alert("This folder does not have any documents.")
}
}
Here is the server side rails code for 'set_current_folder' method
shown above:
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
There is something basic that I am missing which would allow my jQuery
Ajax call to give up control to the server side so that the
"redirect_to documents_path" can execute.
Bharat