Topic: getting javascript return value passed as a parameter into controller

Hi there,

I'm currently trying to use ruby on rails in conjunction with a javascript library. Essentially the library provides a nice UI graph function. The problem I'm currently running into is how to get a return value from a javascript function to pass to my controller to delete an item in my database. My code looks something like this:


//Javascript

function remove_Node()
{
control.deleteNode();
}
function get_node_for_delete()
{
var nodeID = graph.getSelected();
return graph.getNodeText(nodeID);
}

//Ruby

<%= link_to_remote ('destroy', :url => {:action => "destroy"}, :complete => "remove_Node()" ) %>

I've tried to use a :before "get_node_for_delete()" but all it does is run the function, but not return any value. Is there any way to retrieve the javascript return value and pass it into the controller? suggestions?

Thanks

Re: getting javascript return value passed as a parameter into controller

If you want to pass something to the controller you need to define it in the URL parameters. I don't think link_to_remote supports running javascript functions for url parameters, so you'll probably have to do it without the helper. You can see the HTML/JavaScript which is generated by the helper and modify that to add your JavaScript call. Maybe something like:

<a href="#" onclick="new Ajax.Request('/my_controller/destroy?node=' + get_node_for_delete(), {asynchronous:true, evalScripts:true}); return false;">destroy</a>

Railscasts - Free Ruby on Rails Screencasts

Re: getting javascript return value passed as a parameter into controller

Thanks, that worked great!