# Convert Ruby variable to js variable

**URL:** <https://www.ruby-forum.com/t/convert-ruby-variable-to-js-variable/264592>\
**Category:** Development\
**Created:** [February 1, 2024, 10:33am UTC](https://www.ruby-forum.com/t/convert-ruby-variable-to-js-variable/264592 "2024-02-01T10:33:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xlorate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/46a35a/32.png) [@xlorate](https://www.ruby-forum.com/u/xlorate)\
**Post date:** [February 1, 2024, 10:33am UTC](https://www.ruby-forum.com/t/convert-ruby-variable-to-js-variable/264592/1 "2024-02-01T10:33:13Z")

</div>

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

```auto
$modelPath = Sketchup.active_model.path

```

The Html tag

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

```

script

```auto
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

---

<div class="post-metadata">

**Author:** ![robert-b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@robert-b](https://www.ruby-forum.com/u/robert-b)\
**Post date:** [February 1, 2024, 10:33am UTC](https://www.ruby-forum.com/t/convert-ruby-variable-to-js-variable/264592/2 "2024-02-01T10:33:19Z")

</div>

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:

```plaintext
<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

---

<div class="post-metadata">

**Author:** ![xlorate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/46a35a/32.png) [@xlorate](https://www.ruby-forum.com/u/xlorate)\
**Post date:** [February 1, 2024, 2:45pm UTC](https://www.ruby-forum.com/t/convert-ruby-variable-to-js-variable/264592/3 "2024-02-01T14:45:01Z")

</div>

> [@robert-b](#):
>
> In your Ruby file, make sure to set `@modelPath` before rendering the view.

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.
