0

I am using Rails 3. I just want to create a simple javascript object from my controller. I don't want to render js because I'm already rendering html page and I want that javascript object in that html page.

1 Answer 1

3

You want to make data from the controller available in javascript? Then you should use JSON. You can convert any ruby datatype into a json object with the .to_json

Since JSON is valid javascript you can do

var json_object = <%= @ruby_data.to_json %>;
Sign up to request clarification or add additional context in comments.

3 Comments

what's the syntax if I for example I want to make @company available? I know I can do @company.to_json but that's not enough.
Is @company an ActiveRecord object? If so, it should work out of the box.
it's indeed an ActiveRecord object. Conversion works out of the box

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.