1

私は TimelineJS ( http://timeline.verite.co/ ) を使用しており、Rails アプリを使用しています。

そして、TimelineJS で使用する適切な JSON 文字列を生成する方法を見つける必要があります。

以下のデモンストレーション 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

終わり

4

2 に答える 2

0

jbuilderを使用して複雑なJSON出力を作成できます。通常のビューのように機能します。ビューフォルダー内に index/show.json.jbuilder テンプレートを作成するだけです。その後、電話する必要はありませんrender json: @timeline

于 2014-03-19T10:20:52.637 に答える