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