2011-11-22 48 views
1

所以我創建一個簡單的RoR應用程序與evernote的外觀和感覺。RoR創建一個動態的「evernote」 - 頁

notebook.rb

class Notebook < ActiveRecord::Base 
    belongs_to :user 
    has_many :notes 
end 

note.rb

class Note < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :notebook 
end 

和我去前進,創造了一個靜態的控制器索引視圖,這將是我在那裏顯示儀表板

static_controller.rb

class StaticController < ApplicationController 
    def index 
    if user_signed_in? 
     @user = current_User 
     @notebooks = @user.notebooks 
     @notes = @user.notes 
    end 
    end 
end 

我該如何顯示筆記本的列表?每當用戶點擊一個特定的筆記本,該筆記本的筆記顯示在第二列...

我正在考慮爲此創建iframe,但它會更好,如果這些都是divs,我們可以使用jquery動態更新它們......但我仍然沒有想出如何去做。

對不起,聽起來很新奇。

這裏是什麼,我基本上要快照完成

http://i.stack.imgur.com/8gs0l.png

謝謝您的時間!

回答

1

hashify筆記組由它notebook_id

@notes_hash = @ user.notes.group_by(&:notebook_id)

,那麼你的筆記本列表中,點擊筆記本鏈接時,趕上notebook_id和只要看看你的@notes_hash

筆記= @notes_hash [notebook.id] || []

你可以使用jQuery來動態更新你的第二列。

<script type="text/javascript"> 
    var notes = <%= notes.collect{|a| a.note_content }.to_json %>; 

    /* 
    * do stuff here with your array of notes content 
    * 
    */ 

</script> 

確保您需要'json'gem使用.to_json方法。

+0

聽起來不錯!會試試看,謝謝! –