Skip to content

Instantly share code, notes, and snippets.

@robinsp
Created June 12, 2009 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinsp/128779 to your computer and use it in GitHub Desktop.
Save robinsp/128779 to your computer and use it in GitHub Desktop.
<!-- partial -->
<% content_tag :li, :id => dom_id(root) do %>
<%= content_tag :span, root.name %>
<%content_tag :ul do %>
<% for child in root.direct_children do %>
<%= render :partial => "tree", :locals => {:root => child}%>
<%end %>
<%end unless root.direct_children.empty? %>
<%end%>
<!-- index view -->
<h1>Items</h1>
<ul id="my_item_tree">
<%= render :partial => "tree", :locals => {:root => @root} %>
</ul>
<%= sortable_element_reporting_target "my_item_tree", "moved_item_id",
:tree => true,
:url => sort_items_path,
:method => :put %>
# Model
class Item < ActiveRecord::Base
acts_as_nested_set
end
# Controller
class ItemsController < ApplicationController
handles_sorting_of_nested_set
def index
@root = Item.find_by_parent_id(nil)
end
def sort
new_position = position_of(:moved_item_id).in_tree(:my_item_tree)
# This is where you should update your model and
# return som javascript to update the view.
# In this sample, we settle for logging the new position and rendering nothing.
logger.info("New position of item #{params[:moved_item_id]}: " + new_position.inspect)
render :nothing => true
end
end
# routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :items, :collection => {:sort => :put}
end
ul {
list-style: none;
}
li > ul {
padding-left: 1em;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment