1

所以我正在创建一个具有印象笔记外观和感觉的简单 RoR 应用程序。

笔记本.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

然后我继续创建了一个带有索引视图的静态控制器,这将是我显示仪表板的地方

静态控制器.rb

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

我应该如何显示笔记本列表?并且每当用户单击特定笔记本时,该笔记本的笔记就会显示在第二列中...

我正在考虑为此创建 iframe,但如果这些都是 div 会更好,我们可以使用 jquery 动态更新它们......但我仍然没有弄清楚如何去做。

很抱歉听起来很新手。

这是我基本上想要完成的快照

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

感谢您的时间!

4

1 回答 1

1

哈希化笔记以按 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 方法。

于 2011-11-22T12:50:34.997 回答