Convert Ruby variable to js variable

I have a global Ruby variable and need to convert it in js so I can insert it into a tag.
The global variable

$modelPath = Sketchup.active_model.path

The Html tag

<p id = 'file_dir'></p>

script

function get_path() {
  var file_dir = '<%= $modelPath %>'; 
  alert(file_dir);
  document.getElementById("file_dir").value = file_dir;
}

The Alert only shows ‘<%= $modelPath %>’ not the actual string

Hey,

Unfortunately, you can’t directly convert a Ruby variable into a Js variable because they run on different sides of the application (Ruby on the server-side and Js on the client-side).

But you can pass the variable from Ruby to Js via HTML tags. Here’s how:

<script type="text/javascript">
  var file_dir = "<%= @modelPath %>";
</script>

In your Ruby file, make sure to set @modelPath before rendering the view.

In your Js function get_path(), you can directly use file_dir. It now holds the path data from Ruby.

Best Regards,
Bobby

Ah yes, I am calling the html file from a ruby file, so not sure how I would pass the @modelPath to the html file.