Skip to content

Instantly share code, notes, and snippets.

@ravinggenius
Created January 3, 2011 00:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravinggenius/762969 to your computer and use it in GitHub Desktop.
Save ravinggenius/762969 to your computer and use it in GitHub Desktop.

I have a Rails 3 application; we can consider it a blog system. All content is stored in Nodes. I am using a specifically tailored route to allow Nodes to have clean URLs.

= form_for [:admin, @node] do                        # => generated URL is /admin/nodes or /admin/nodes/4d1a44571d41c821eb000006
= link_to 'all nodes', nodes_path                    # => generated URL is /
= link_to @node.title, node_path(@node.to_path)      # => generated URL is /about or /about/people or /4d1a44571d41c821eb000006
= link_to 'all nodes (admin view)', admin_nodes_path # => generated URL is /admin/nodes
= link_to 'add', new_admin_node_path                 # => generated URL is /admin/nodes/new
= link_to 'edit', edit_admin_node(@node)             # => generated URL is /admin/nodes/4d1a44571d41c821eb000006/edit
= link_to 'delete', [:admin, @node], :confirm => 'Are you sure?', :method => :delete
                                                     # => generated URL is /admin/nodes/4d1a44571d41c821eb000006
class Node
include MongoMapper::Document
key :path, String, :unique => true
key :title, String, :required => true
def to_path
path.to_s.blank? ? _id.to_s : path
end
def self.from_path(path)
return nil if path.nil? || path.blank?
reply = first :path => path
reply = find path unless reply.present?
reply
end
end
Blog::Application.routes.draw do
resources :nodes, :only => :index, :show
namespace :admin do
resources :nodes, :except => :show
end
get '/*id_or_path', :to => 'nodes#show', :as => :node
root :to => 'nodes#index'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment