Sharjeel
Sharjeel

Reputation: 15798

Rails: How to create javascript object from controller?

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.

Upvotes: 0

Views: 1912

Answers (1)

Tarscher
Tarscher

Reputation: 1933

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 %>;

Upvotes: 3

Related Questions