Created
September 18, 2009 22:29
-
-
Save hallison/189331 to your computer and use it in GitHub Desktop.
Example of how to use Sinatra::Mapping
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'sinatra' | |
require 'sinatra/mapping' # only this line for use mapping! | |
map :root, "blog" # /blog/ | |
map :entries, "posts" # /blog/posts | |
map :tags, "labels" # /blog/labels | |
mapping :entry => "posts/:entry_id", # /blog/posts/id-for-post | |
:entry_comments => "posts/:entry_id/comments", # /blog/posts/id-for-post/comments | |
:tagged_entries => "labels/:tag_id/entries" # /blog/labels/id-for-tag/entries | |
# /blog/ | |
get root_path do | |
<<-end_content | |
<h1>Welcome to Foo Web Application</h1> | |
<ul> | |
<li>#{link_to title_path(:entries), :entries, :title => title_path(:entries)}</li> | |
<li>#{link_to title_path(:tags), :tags, :title => title_path(:tags)}</li> | |
</ul> | |
end_content | |
end | |
# /blog/entries | |
get entries_path do | |
<<-end_content | |
<h1>Welcome to Foo Web Application</h1> | |
<h2>#{title_path(:entries)}</h2> | |
<ul> | |
<li>#{link_to "Testing new entry ...", :entries, "testing-new-entry"}</li> | |
<li>#{link_to "Testing old entry ...", :entries, "testing-old-entry"}</li> | |
</ul> | |
<p> | |
#{link_to "Back", :root} | |
</p> | |
end_content | |
end | |
# /blog/labels/tag-id-for-show-content | |
get tags_path do | |
<<-end_content | |
<h1>Welcome to Foo Web Application</h1> | |
<h2>#{title_path(:tags)}</h2> | |
<ul> | |
<li>#{link_to "Ruby", :tags, "ruby", "entries"}</li> | |
<li>#{link_to "Sinatra", :tags, "sinatra", "entries"}</li> | |
<li>#{link_to "Mapping", :tags, "mapping", "entries"}</li> | |
</ul> | |
<p> | |
#{link_to "Back", :root} | |
</p> | |
end_content | |
end | |
# /blog/entries/entry-id-for-show-content | |
get entry_path do |entry_id| | |
title = entry_id.gsub('-',' ').capitalize | |
<<-end_content | |
<h1>Welcome to Foo Web Application</h1> | |
<h2>#{title}</h2> | |
<p> | |
It works! | |
</p> | |
<p> | |
#{link_to "Back", :root} | #{link_to "Comments", :entries, entry_id, 'comments'} | |
</p> | |
end_content | |
end | |
# /blog/entries/entry-id-for-show-content/comments | |
get entry_comments_path do |entry_id| | |
title = entry_id.gsub('-',' ').capitalize | |
<<-end_content | |
<h1>Welcome to Foo Web Application</h1> | |
<h2>#{title}</h2> | |
<p> | |
It works and show comments for "#{title}". | |
</p> | |
<p> | |
#{link_to "Back", :root} | |
</p> | |
end_content | |
end | |
get tagged_entries_path do |tag_id| | |
<<-end_content | |
<h1>Welcome to Foo Web Application</h1> | |
<h2>#{tag_id.capitalize}</h2> | |
<p> | |
It works and show all entries tagged with "#{tag_id}". | |
</p> | |
<p> | |
#{link_to "Back", :root} | |
</p> | |
end_content | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment