Lu Liu
2009-Oct-19 15:47 UTC
problems with calling javascript containing dynamic content in partial page
Hi all, I am trying to call some javascript function defined in partial page "_results.html.erb". But it seemed that the js functions in the page are unrecognizable, and I have no idea why. In my app, I have an "index" action/view containing an form_remote_tag sending the ajax request to a "process" action. After some calculation in the "process" action, it passes a local variable to a result page and renders it partially. Briefly, the index.html.erb looks like: # index.html.erb <% form_remote_tag :url => ''process'', :id => ''inputs'', :update => {:success => ''result_div'', :failure => ''document.write(request.responseText)''} do -%> # some input tags here <%= submit_tag "calculate" %> <% end -%> <div id = ''result_div''></div> In controller#index, there is nothing. In controller#process, there is the calculation code which will cost several seconds, after that it is the render method: def process # calculation code which will generate a result - @ruby_result - which is an ruby object render :partial => ''results'', :locals => {:ruby_result => @ruby_result} end And in _results.html.erb: <a href = "javascript:showResult()">show result</a> <script type="text/javascript"> function showResult() { // code to show the result somewhere else on the index page // BUT I MUST use the <%=ruby_result%> variable here } </script> The update of the result_div is totally ok, but when I click on the "show result", nothing happened. I tried putting the showResult() function in the index.html.erb. Then it can be found by the _results.html.erb. However the problem is that the ruby_result can only be got after the process. As a result, it''s not accessible in the index view when it is initially loaded. How should I solve this problem? I am not sure if I have expressed my question clearly. Any reply or comment is welcome. Thanks!