Topic: How to pass a Json object from ruby code to javascript?

I am using a Javascript library for represent the content of JSON in visual way however I need to put it in database too with ruby code o I need to get JSON by ruby then pass it to Javascript or vice a versa. Which is more plausible?

Code me tender code me sweet...

Re: How to pass a Json object from ruby code to javascript?

Where does the json come from originally?  Does it come from an external source?

Here's a good read:

http://mike.bailey.net.au/2011/02/json- … and-rails/

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to pass a Json object from ruby code to javascript?

It is external web service. I get it currently by XHR by Javascript

Code me tender code me sweet...

Re: How to pass a Json object from ruby code to javascript?

However now on I need to get JSON by ruby code than pass it to javascript as JSON object

Code me tender code me sweet...

Re: How to pass a Json object from ruby code to javascript?

Can you change your app,  and let a controller grab the JSON?

I think that would be easier than the other way around, letting javascript retrieve the JSON,  but you can do it either way.  If you want the retrieved json parsed and put in a Rails ActiveRecord model,  it would be much simpler to let rails retrieve the json,  put it in a rails model,  then let a controller convert the model record to json for hand off to the javascript that needs json,  but it's a chicken/egg 6 of one/ half a dozen of another type problem.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to pass a Json object from ruby code to javascript?

BradHodges wrote:

Can you change your app,  and let a controller grab the JSON?

I think that would be easier than the other way around, letting javascript retrieve the JSON,  but you can do it either way.  If you want the retrieved json parsed and put in a Rails ActiveRecord model,  it would be much simpler to let rails retrieve the json,  put it in a rails model,  then let a controller convert the model record to json for hand off to the javascript that needs json,  but it's a chicken/egg 6 of one/ half a dozen of another type problem.

After that is it possible to pass the JSON from ActionRecord model to Javascript

Code me tender code me sweet...

Re: How to pass a Json object from ruby code to javascript?

You last post came while I composing my last post.

The easiest thing is to use the Ruby URI stuff to do the post and retrieve the json.

http://www.ruby-doc.org/stdlib-1.9.3/

Then use ActiveSupport::JSON:decode  to the decode retrieved data and put it in a ruby object.  Depending on the format of the json, if you're jucky,  you get a ruby hash that resembles a normal hash needed by ActiveRecord,  i.e. of the hash comes back like this:

{ 'name' => 'fred', 'address' => '123 West St' }

And you have a ActiveRecord Model (database table) with a name and address attribute

Then it would be  simple matter of

@thing = Thing.new(ActiveSupport::JSON:decode(data))
@thing.save

data would be what you got back in the response body you got back from the request you sent using the URI stuff.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to pass a Json object from ruby code to javascript?

We're out of sync!

Yes,  suppose you save it in a model first,  then you use the new assets pipeline feature of rails,  you'd put your javascript in app/assets/javascripts/yourjavascript.js.erb

NOTICE the .erb extension

Now you can do something like this

yourjavascript.js.erb

$('#mydiv').html(<%= escape_javascript(@thing.name) %>);

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to pass a Json object from ruby code to javascript?

$('#mydiv').html('<%= escape_javascript(@thing.name) %>');

forgot the quotes!

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to pass a Json object from ruby code to javascript?

I didn't answer you question exactly,

So when you use URI to call to your external source, and get back the Json in response body data,  save that as an instance variable in the controller,  say you call it @jsondata,  then

myjsthing_expectingjsonarg(<%= escape_javascript(@jsondata) %>);

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to pass a Json object from ruby code to javascript?

Thank you so much you solve my problem for sure smile Thanks

Code me tender code me sweet...