Reputation: 3390
So I'm using TimelineJS (http://timeline.verite.co/) and I'm in a Rails app.
And I need to find out how to generate a suitable JSON string for use with TimelineJS.
So given the demonstration JSON string below.....what do I need to do to my app in order to recreate this JSON string?
(https://github.com/VeriteCo/TimelineJS#json) The example JSON the TimelineJS developers demonstrate looks like so...
var dataObject = {
"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",
"classname":"optionaluniqueclassnamecanbeaddedhere",
"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"
}
]
}
}
Not sure if this helps but, the schema for my app looks like so....
ActiveRecord::Schema.define(:version => 20130507031759) do
create_table "ckeditor_assets", :force => true do |t|
t.string "data_file_name", :null => false
t.string "data_content_type"
t.integer "data_file_size"
t.integer "assetable_id"
t.string "assetable_type", :limit => 30
t.string "type", :limit => 30
t.integer "width"
t.integer "height"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "ckeditor_assets", ["assetable_type", "assetable_id"], :name => "idx_ckeditor_assetable"
add_index "ckeditor_assets", ["assetable_type", "type", "assetable_id"], :name => "idx_ckeditor_assetable_type"
create_table "coins", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "coin_id"
end
create_table "events", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "moderations", :force => true do |t|
t.integer "moderatable_id"
t.string "moderatable_type"
t.text "data", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "mods", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "pages", :force => true do |t|
t.text "content"
t.string "name"
t.string "permalink"
t.string "image_url"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "posts", :force => true do |t|
t.string "coin_id"
t.text "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "title"
t.boolean "approved"
t.string "photo_file_name"
t.string "photo_content_type"
t.integer "photo_file_size"
t.datetime "photo_updated_at"
t.integer "event_id"
end
create_table "users", :force => true do |t|
t.string "email"
t.string "password_digest"
t.boolean "admin"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "spree_api_key", :limit => 48
t.integer "ship_address_id"
t.integer "bill_address_id"
t.string "auth_token"
t.string "password_reset_token"
t.datetime "password_reset_sent_at"
t.string "username"
t.boolean "avatar"
end
end
So how would I go about creating a TimelineJs JSON string from this? Any ideas?
UPDATE: using advice on first answer provided below...I have this in my model and controller files respectively...but this is not working...I'm getting an "undefined method - timeline" error...what am I doing incorrectly?
coin.rb (model)
class Coin < ActiveRecord::Base
attr_accessible :coin_id
has_many :events
has_many :users, :through => :events
def timeline
t = {}
t['timeline'] = {}
t['timeline']['headline'] = "Lorem"
t['timeline']['text'] = "default"
t['timeline']['asset'] = ""
t['timeline']['asset']['media'] = ""
t['timeline']['asset']['credit'] = ""
t['timeline']['asset']['caption'] = ""
t['timeline']['date'] = {}
t['timeline']['date']['startDate'] = ""
t['timeline']['date']['endDate'] = ""
t['timeline']['date']['headline'] = ""
t['timeline']['date']['text'] = ""
t['timeline']['date']['tag'] = ""
t['timeline']['date']['classname'] = ""
t['timeline']['date']['asset'] = ""
t['timeline']['era'] = {}
t['timeline']['era']['startDate'] = ""
t['timeline']['era']['endDate'] = ""
t['timeline']['era']['headline'] = ""
t['timeline']['era']['text'] = ""
t['timeline']['era']['tag'] = ""
return t
end
end
And inside coin_controller.rb
class CoinsController < ApplicationController
# GET /coins/1
# GET /coins/1.json
def show
@coin = Coin.find(params[:id])
@timeline = Coin.timeline.to_json
respond_to do |format|
format.html # show.html.erb
format.json { render json: @timeline }
end
end
end
Upvotes: 1
Views: 4612
Reputation: 530
You can use jbuilder to create complex JSON
output. It works like a normal view. Just create a index/show.json.jbuilder template inside your views folders. Then you don't need to call render json: @timeline
.
Upvotes: 0
Reputation: 152
Rails has a built in to_json method. What you'll need to do is use a method in your model that creates a hash with this structure, and you can send it to the front end as json. For example:
class YourModel
def timeline
t = {}
t['timeline'] = {}
t['timeline']['headline'] = "Lorem ipsum"
...
...
return t
end
end
In your controller action:
@timeline = model.timeline.to_json
From there, you'll need to call JSON.parse() to consume the data on the front-end.
Upvotes: 5