I'm trying to create a timeline, with a form for a user to input a new event. I've decided to use the timeline from http://timeline.verite.co/ - I can create a form for each user to input a new event, and save that event to the database no problem. However, what I'm having trouble figuring out is:
a) Getting the data from the database and turning it into the correct JSON format that the timeline would be happy with - I have no idea where to start with this! Do I need to name the columns in the database with the same name as the JSON attributes, e.g. "caption"? And how do I then turn this into JSON?
b) How do I generate the JSON doc on the fly? The code requires a link to a JSON doc with the data.
This is the only way I can figure out how to do this - as if I was to generate a JSON doc for each user and insert each new event, then read from this, then surely there is some security issues with a JSON doc being easier to download by someone who doesn't own it, than data in a database?
Any help or pointers would be much appreciated - I'm pretty new to Rails and haven't had to really touch JSON yet! Thank you.
In case it helps, the data format required by the timeline is this:
{
"timeline":
{
"headline":"The Main Timeline Headline Goes here",
"type":"default",
"text":"<p>Intro body text goes here, some HTML is ok</p>",
"asset": {
"media":"http://yourdomain_or_socialmedialink_goes_here.jpg",
"credit":"Credit Name Goes Here",
"caption":"Caption text goes here"
},
"date": [
{
"startDate":"2011,12,10",
"endDate":"2011,12,11",
"headline":"Headline Goes Here",
"text":"<p>Body text goes here, some HTML is OK</p>",
"tag":"This is Optional",
"asset": {
"media":"http://twitter.com/ArjunaSoriano/status/164181156147900416",
"thumbnail":"optional-32x32px.jpg",
"credit":"Credit Name Goes Here",
"caption":"Caption text goes here"
}
}
],
"era": [
{
"startDate":"2011,12,10",
"endDate":"2011,12,11",
"headline":"Headline Goes Here",
"text":"<p>Body text goes here, some HTML is OK</p>",
"tag":"This is Optional"
}
]
}
}
data_hash.to_json. How to put data from the DB into hash is another question, it depends much on your DB structure and your knowledge of Ruby and ActiveRecord.to_json.