2011-11-22 46 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

听起来不错!会试试看,谢谢! –