Surya
Surya

Reputation: 2699

Ruby on Rails as a RESTful architecture

Whenever I read about REST architecture, there is always a word about stateless operations.From my basic understanding Ruby on Rails is supposed to be of REST architecture. From my understanding stateless operations means consecutive requests are independent of each other. But in Ruby on Rails we do have session variables and cookies that sort of keeps track of some data from one request to another. Doesn't this defy stateless operations? I am confused. Can someone please clarify?

Upvotes: 1

Views: 836

Answers (2)

max
max

Reputation: 102164

Ruby on Rails follows a pragmatic real world approach - not a fundamentalist adherence to principle. REST may be stateless at its core, but creating completely stateless applications for the browser is not very desireable.

Rails mostly embraces the ideas of resource identifiers using HTTP verbs to signify different actions from REST in its routing conventions. How you use it build applications is up to the programmer.

# An example of Rails "RESTful" routes:

    Prefix Verb   URI Pattern                Controller#Action
    things GET    /things(.:format)          things#index
           POST   /things(.:format)          things#create
 new_thing GET    /things/new(.:format)      things#new
edit_thing GET    /things/:id/edit(.:format) things#edit
     thing GET    /things/:id(.:format)      things#show
           PATCH  /things/:id(.:format)      things#update
           PUT    /things/:id(.:format)      things#update
           DELETE /things/:id(.:format)      things#destroy

API applications that use token based authentication can be considered stateless for example since the clients credentials are passed in a HTTP header with every request.

For classical web applications that is not feasible since the browser does not provide any useful options of sending credentials with the request. You can build a completely stateless application - but not with any sort of authentication without requiring the user to sign in on every request.

What about cookies?

Cookies are indeed sent with every request - and where for a long time the only way the browser could hold state aside from passing it back and forth via URL:s.

However the way that Rails uses a cookie for session storage is more incidential than to give it a stateless nature. Decoding an encrypted cookie is simply faster than any other storage mechanism.

The whole idea of sessions is stateful in nature. You are adding a state storage aside from the request url, params and headers which is why session based authorization is considered stateful.

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230386

But in Ruby on Rails we do have session variables and cookies that sort of keeps track of some data from one request to another. Doesn't this defy stateless operations?

No, the requests remain "stateless" (with cookie-based session storage), because cookies are sent with every request (instead of being tracked on the server). Therefore, each request contains all info required for its processing, making it independent from all previous requests.

Upvotes: 4

Related Questions