ZX12R
ZX12R

Reputation: 4826

How to use instance variables in rails ujs?

This is what i used to do in html.erb earlier:

my markup
<script type="text/javascript">
    var somevalueFromInstance = "<%= @value_from_instance %>";
    // code directly using the someValueFromInstance
</script>
markup continues..

mostly i will place the script tag in a content_for block.

Now when i want to do this unobtrusive, i cannot have any script tags in the html.erb. The script lies in a javascript file. But how do i transport the @value_from_instance to the javascript file?

Is storing it in a data-attribute the only option to do this?

Also i want to do this on page load and not in an ajax request for this purpose

Upvotes: 0

Views: 273

Answers (1)

shingara
shingara

Reputation: 46914

The data-* attribute is do to that. It's the must simplest way to define your args or you can create some variable in a script tag in your HTML directly.

Upvotes: 1

Related Questions