· 1 min read

Screencast: Daten-Bäume mit Ancestry verwenden

Download:

Download(15.7 MB, 9:39) Alternativer Download für iPod & Apple TV(15.1 MB, 9:39)

Resourcen:

Quellcode:

[bash] bundle rails g migration add_ancestry_to_messages ancestry:string rake db:migrate [/bash]

[ruby] # Gemfile gem ‘ancestry’

# migration.rb class AddAncestryToMessages < ActiveRecord::Migration def self.up add_column :messages, :ancestry, :string add_index :messages, :ancestry end

def self.down remove_index :messages, :ancestry remove_column :messages, :ancestry end end

# messages_controller.rb def new @message = Message.new(:parent_id => params[:parent_id]) end

# messages_helper.rb def nested_messages(messages) messages.map do |message, sub_messages| render(message) + content_tag(:div, nested_messages(sub_messages), :class => “nested_messages”) end.join.html_safe end [/ruby]

[html] <%= nested_messages @messages.arrange(:order => :created_at) %>

<%= nested\_messages @message.subtree.arrange(:order => :created\_at) %> <%= f.hidden\_field :parent\_id %> <%= link\_to message.content, message %> ... <%= link\_to "Reply", new\_message\_path(:parent\_id => message) %> | \[/html\]

[css] /* application.css */

.nested_messages { margin-left: 30px; }

.nested_messages .nested_messages .nested_messages .nested_messages { margin-left: 0; } [/css]

Back to Blog