passing a javascript variable to ruby in a view - possible?

Hello,

I have a simple application that allows you to create an account, sign in, and play a game using an embedded web player on an .rhtml page. The web player (Unity) is able to call javascript functions on the .rhtml page. The .rhtml page uses a partial to render a high score list.

I want to be able to refresh the current high score list based on the level you are on in the game. The game calls the following JS when a level is cleared:

<script language="javascript1.1" type="text/javascript"> function loadHighScores(level) {   //this won't work because the button is invisible in JS and because level is not accessible to Ruby         //            <% form_remote_tag :url => { :action => :update_scores, :id => level } do %>             <%= submit_tag "Refresh Scores for This Level" %>       <% end %> } </script>

So is there a way to: a) pass the JS level variable to Ruby b) perform the remote call without the user having to press a button; in other words, only when the JS function loadHighScores is called by the external game player (I would like to avoid the function periodically_call_remote if possible)

My apologies if this post belongs in one of the spin-off groups (AJAX).

Thanks, n

Just create an ajax request and allow a parameter to be passed in

<script type="text/javascript"> //<![CDATA[ function update_my_method(a_value) {   new Ajax.Request('/my_controller/my_method/'+a_value,           {asynchronous:true, evalScripts:true})

} //]]> </script>

i don't think that there is currently a helper to do this. This quick and dirty helper (based on javascript_tag and code from remote_function) will create a simple id value.

def pass_to_remote(options, function_name)   javascript_options = options_for_ajax(options)   function = "function #{function_name}(value){"   function<< "new Ajax.Request("      url_options = options[:url]      url_options = url_options.merge(:escape => false) if url_options.is_a?(Hash)      function << "'#{url_for(url_options)}/'+value)"      function << ", #{javascript_options}"      function<< "}"     return javascript_tag(function) end

To use in the view something like. <%= pass_to_remote({:url=>{:action=>'change_something'}}, 'remote_change') %> will create a javascript function called 'remote_change', only url option is included, and this should only contain action and controller.

To test: (obviously you would have javascript code that can call remote_change directly). <a href='#' onclick='remote_change(8)'>hello</a>

This works. I have also tried extending the helper to allow a name for the value to be included so that it appears with that name in the params. But this is a bit more tricky and I havn't quite got it right yet, separating the named value and the id value is not quite straightforward.

hth Tonypm

You can do this with :with

<script > jsparams={person_name: 'bob' quantity: 2}; </script> <%= link_to_remote 'foo', :url => {:action => 'index'}, :with => 'jsparams'%>

Clicking on the link results in

Processing FooController#index (for 127.0.0.1 at 2007-10-06 18:55:44) [POST]    Session ID: cd593ad4bfc50ff48ce34d7fa4e0d7d1    Parameters: {"person_name"=>"bob", "quantity"=>"2", "action"=>"index", "controller"=>"foo"}

:with can be pretty much any javascript expression.

Fred