Created
March 28, 2012 14:52
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'acts_as_tree' | |
gem "friendly_id", "~> 4.0.1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PagesController < ApplicationController | |
def show | |
@page = Page.find_by_path(params[:path]) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Whatever::Application.routes.draw do | |
# every other route... | |
match "*path", :to => "pages#show" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!