0

I am trying to access Ruby class attributes in a JS/JQ file.

Example:

class my_class
  attr_acessor :my_attr
end

In the Javascript, how can I access and modify this?

$('my_class').???

1 Answer 1

1

ANSWER FOR UPDATED QUESTION

You cannot modify a Ruby object from JavaScript.

ANSWER FOR ORIGINAL QUESTION

You need to embed your Ruby code in JavaScript. Your js/html file must end in js.erb or html.erb.

Here is a quick example:

<script>
  var javascript_variable_attr = <%= @instance_of_my_class.my_attr %>
</script>

And FYI you cannot modify the Ruby object because you are not accessing the object in memory. Embedding Ruby will simply render the value into the js/html file. In other words, it is read only.

Sign up to request clarification or add additional context in comments.

2 Comments

There is no templating engine, save for erb, huh?
There is mustache if you don't like erb. It's much cleaner. But at a low level it's basically the same thing. github.com/mustache/mustache

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.