Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing objects from Rails RJS to a javascript function call without quoting the values?
    text
    copied!<p>In a Ruby on Rails project I need to have a JavaScript function executed at the end of an Ajax call. My problem is that the values being passed into the JavaScript function being called with page.call are being wrapped up in quotes. Not too much of a problem if you're passing in strings but one of the values is a string representation of a JavaScript Array, i.e. [0,1,2,3].</p> <p>Here are what I feel are the important snippets of code.</p> <p>I have an observe_field that watches a select drop down list.</p> <pre><code>&lt;%= observe_field("project_select_list", :loading =&gt; "Element.show('tree_indicator')", :complete =&gt; "Element.hide('tree_indicator')", :url =&gt; {:controller =&gt; "projects", :action =&gt; "change_project"}, :with =&gt; "id") %&gt; </code></pre> <p>In the project_controller</p> <pre><code>def change_project() @current_project = Project.find(params[:id]) end </code></pre> <p>And in change_project.rjs</p> <pre><code>page.replace_html("project_name", @current_project.name) page.visual_effect(:highlight, "project_name") page.call("buildYUITreeView", "project_tree", @current_project.get_directory_contents(@current_project.local_path, 0)) </code></pre> <p>The last value:</p> <pre><code>@current_project.get_directory_contents(@current_project.local_path, 0)) </code></pre> <p>is what's causing me issues. I just want it to send the value, for example [0,1,2,3], but it's sending "[0,1,2,3]" which is causing the JS to blow up.</p> <p>This partial does what it's supposed to in that it sends the data and not a string to the JavaScript code that gets put on the page.</p> <pre><code>&lt;% javascript_tag do -%&gt; YAHOO.util.Event.addListener("dom:loaded", "load", buildYUITreeView("&lt;%= tree_id %&gt;", &lt;%= project.get_directory_contents(project.local_path, 0) %&gt;)); &lt;% end -%&gt; </code></pre> <p>With this in mind I'm playing around with using a partial and just rendering it to call the JS function when I need to but that seems like such a hack.</p> <p>How can I make page.call not wrap the data sent as parameters in quotes, or how should I go about passing this data to the JS function for execution after the Ajax call is complete?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload