REST in rails

Rails introduced support for designing APIs consistent with the REST style. Representational State Transfer (REST) is complex topic in Information theory. Basically, you start with the general proposition of machines that can talk to each other, and you start ruling some practice in and others out by imposing constraints that include:

  • Use of a client-server architecture
  • Stateless communication
  • Expicit signaling of response cacheability
  • Use of HTTP request methods such as GET, POST, PUT and DELETE

REST in Rails

the REST support in Rails consists of methods to define resources in the routing system, designed to impose a particular style and order and logic on your controllers and, consequently, on the way the world sees your application.

The story of REST and Rails starts with CRUD

Routing and CRUD

CRUD ( Create Read Update Delete) is the classic summary of the spectrum of database operations. the routing system does not force you to implement your app’s CRUD functionality in any consistent manner.

Here’s the logic. It’s good to give CRUD-based names to your actions. It’s convenient and elegant to use named routes.  The REST support in rails gives you named routes that point to CRUD-based action names. if you put into your config/routes.rb file,n you will have created for named routes.

by creating a route like bellow:

config/routes.rb

  root :to => 'pages#home'
  match '/about', :to => 'pages#about'
  match '/contact', :to => 'pages#contact'

what’s the meaning?

This code maps the root URL / to /pages/home, and also gives URL helpers as follows:
root_path => ‘/’
root_url => ‘http://localhost:3000/’

Named route

<%= link_to "Home", root_path %>
<%= link_to "About", about_path %>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s