Recently for a project I needed to integrate thickbox to display some content. Shortly after I got thickbox working correctly I discovered that I needed to add authentication to the panel I was displaying. My problem was how to catch the intended url and then redisplay it after the user had logged in, oh and of course I wanted to do this all with AJAX.
Once I got the authentication call working correctly I ran into a problem with rendering the intended action back to the form within a single ajax call. My first attempt was rendering a partial to a string and then passing the string back. It looked something like this.
rt = render :partial => "#{session[:intended_controller]}/#{session[:intended_action]#}"
render :update do |page|
page.replace_html 'modal_login', rt
end
The problem with this attempt was that the partial required database calls and things that were being generated within the controllers action. So what I really needed to do was call the other controllers action with the "params" hash from the original call.
After looking through all the render helpers I stumbled across the
render_component method. This seems to do the trick. Since I captured the original controller and action, I can simply call this and store the result in a variable. So I changed my two lines to look like the following:
rt = render_component :controller=> session[:intended_controller], :action => session[:intended_action]
render :update do |page|
page.replace_html 'modal_login', rt
end
Now this generated call the controller and action, but I still had two problems. My first problem was in passing the original "params" hash to the action. My second problem occurred due to the component call causing a double render error. The first problem is solved by adding another option to the render component call :params => session[:intended_params]. The second problem was solved when I found a bug ticket referencing the render_component_as_string method.
After changing this last two things I was able to get controller action rendered correctly and returned to my thickbox window via ajax. Below is the final code that I placed in my ajax authentication routine.
rt = render_component_as_string(:controller=> session[:intended_controller], :action => session[:intended_action], :params => session[:intended_params])
render :update do |page|
page.replace_html 'modal_login', rt
end
Comments