Clone this wiki locally
Actually it’s a set of templates to introduce RESTful routes in Sinatra. The
only thing for you to do is to provide the views behind the routes.
Guess what!sudo gem source —add http://gems.github.com sudo gem install blindgaenger-sinatra-rest
Of course you need to require the gem in your sinatra application:require ‘rubygems’ require ‘sinatra’ require ‘sinatra/rest’
It’s very similar to defining routes in Sinatra (
post, …). But this
time you don’t define the routes by yourself, but use the model’s name for
For example, if the model’s class is called
Person you only need to add this
Which will add the following RESTful routes to your application. (Note the
Person to the
|POST||/people||create||redirect to show|
|PUT||/people/1||update||redirect to show|
|DELETE||/people/1||destroy||redirect to index|
As you can see, each route is also associated with a named code block in the
controller. The controller does the minimum to make the routes work. Based on
the route it treates the model and redirects or renders the expected view.
So imagine the following steps to show a single person:
- request from the client’s browser
- Find and set @person in the controller
@person = Person.find_by_id(99)
- render the view to display
It’s up to you to provide the views, because this goes beyond the restful
routing. The variable
@person is correctly named and injected into the view.
So maybe you’d like to do something like this:
<html> <body> <div>ID: <%= @person.id %></div> <div>Name: <%= @person.name %></div> </body> </html>
You can contact me via mail at blindgaenger at gmail dot com, or leave me a
message on my Github profile.