Passing data from WebDialog to Ruby

How to send more then only one parameter from WebDialog to Ruby?

function callRuby(htmlpage) {
// Declare the variable with “var” so that it is not global.
var query = ‘skp:pass_data@’ + htmlpage;
window.location.href = query;
}

How to write line: var query = 'skp:pass_data@’ + htmlpage;

Please use the ```javascript or ```ruby blocks to surround code in forum posts.


In the <head> element, within the <script> element:

// Call Ruby with a parameter string of comma separated values:
function callRuby(params) {
    // Declare the variable with "var" so that it is not global.
    var query = 'skp:pass_data@' + params;
    window.location.href = query;
}

Somewhere in the HTML body or another JS function send some data:

callRuby('Mamjanowski,48,5.678,etc.,I love SketchUp');

In Ruby:

@dlg.add_action_callback("pass_data") {|dlg,csv|
  param_array = csv.split(',') rescue []
  # use your param_array of passed data
  puts param_array
}

>> outputs:

["Mamjanowski", "48", "5.678", "etc.", "I love SketchUp"]

Basically, you can use whatever delimiter you choose, not just a comma. Use a semi-colon ';' if you prefer.


Some coders send JSON strings, but that needs to also send all the fieldnames, which is not always needed.

1 Like