所以我正在使用 TimelineJS ( http://timeline.verite.co/ ) 并且我在 Rails 应用程序中。
而且我需要了解如何生成一个合适的 JSON 字符串以与 TimelineJS 一起使用。
因此,鉴于下面的演示 JSON 字符串.....我需要对我的应用程序做什么才能重新创建此 JSON 字符串?
( https://github.com/VeriteCo/TimelineJS#json ) TimelineJS 开发人员演示的示例 JSON 看起来像这样......
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"
}
]
}
}
不确定这是否有帮助,但我的应用程序的架构看起来像这样......
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
那么我将如何从中创建一个 TimelineJs JSON 字符串呢?有任何想法吗?
更新:使用下面提供的第一个答案的建议......我的模型和控制器文件中分别有这个......但这不起作用......我收到“未定义的方法 - 时间线”错误......这是什么我做错了吗?
coin.rb (模型)
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
在 coin_controller.rb 里面
类 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
结尾