Skip to content

Instantly share code, notes, and snippets.

@baldwindavid
Created March 28, 2012 14:52
Show Gist options
  • Save baldwindavid/2226824 to your computer and use it in GitHub Desktop.
Save baldwindavid/2226824 to your computer and use it in GitHub Desktop.
The guts of a page tree using slugs - acts_as_tree, friendly_id - Allows for routes like /about/our-people/developers
gem 'acts_as_tree'
gem "friendly_id", "~> 4.0.1"
class Page < ActiveRecord::Base
acts_as_tree
extend FriendlyId
friendly_id :title, :use => :scoped, :scope => :parent
before_save :compute_path
def compute_path
self.path = ancestors.reverse.push(self).collect(&:slug).join("/")
end
end
# == Schema Information
#
# Table name: pages
#
# id :integer not null, primary key
# title :string(255)
# creator_id :integer
# content :text
# parent_id :integer
# position :integer
# path :string(255)
# slug :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class PagesController < ApplicationController
def show
@page = Page.find_by_path(params[:path])
end
end
Whatever::Application.routes.draw do
# every other route...
match "*path", :to => "pages#show"
end
@janxious
Copy link

Nice!

@johanbaaij
Copy link

Useful. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment