Ruby on Rails: A Definitive Explanation of REST as it Relates to Rails, Crud, and Restful Routing

Ryan Kopf
In case you've missed it, Ruby on Rails is the latest miracle to hit the web programming world, mixing the beauty of Ruby with a strict, convention-based development framework. Ruby on Rails helps software developers create agile programs that can be changed to suit the needs of the moment - much unlike standard software development which creates programs that are bulky, clunky, and hard to expand upon.

One of the more recent developments with ruby on rails is called REST. REST is basically an expansion on the basics of CRUD. CRUD, if you don't know already, means Create, Read, Update, Destroy. Whenever you use scaffolding to create rails files, you automatically find things like create, show, update, and destroy. HTTP comes with PUT/GET/POST/DELETE, which equal CREATE/READ/UPDATE/DELETE. This is why when somebody posts a form to /user/1, it will update the value of user with id=1 - rails knows automatically that a 'post' request means you will be updating something.

RESTful development expands this, to also provide Index, New, and Edit. But REST is also a philosophy, in which adjectives (new,edit,index) are considered views, and verbs(create,read,update,destroy) are actions. Verbs come before the object id, and adjectives come after. Consider these restful URLs:
GET /profile/1 - renders a display of profile id=1
GET /profile/1/edit - renders a display of an edit form for profile=1 (notice that edit is an adjective, modifying what is displayed)
GET /profile/index - renders an index of profiles
POST /profile/1 - edits profile id=1
PUT /profile/ - creates a new profile
DELETE /profile/1 - deletes profile id=1

These 'routes' as the URLs are called in rails, are what make RESTful programming important. Basically it's more convention and less configuration. You automatically have index, new, edit, create, show, update, destroy all created automatically for you. If you need to do anything else, you can just add it yourself, but most people rarely deviate from these specifications.

You also get access to helpers, to generate paths for you. "" will generate a link to "/profile/1" pointing to the specific profile in the @profile variable. To have these automatically generated for you, open up routes.rb and add "map.resources :profiles" to the top. This will add these default URL specifications, giving you easy access to RESTful routes.

Published by Ryan Kopf

Ryan is a technologist and geek who organizes anime conventions through the magic of technology and an awesome team of evil super-villains. He graduated with an AA in 2008, is studied for a BA in computer sc...  View profile

  • Ruby on Rails is flexible and cuts development time
  • REST is one of the latest Rails features
  • Convention makes for less time wasted configuring settings
With Ruby on Rail's convention over configuration, software developers save a lot of time. Many of the features their program will need are created by default when using RESTful techniques.

To comment, please sign in to your Yahoo! account, or sign up for a new account.