RESTful

What is the advantages of a RESTful design in Rails?
How can I make sure my design is RESTful?

“REST” stands for “Representational State Transfer”, and it’s a way of
using more than just HTTP’s GET and POST methods to conduct operations
on a resource.

This might best be explained with an example. Let’s say you have a
database full of products. Maybe they have a title, a price and a
picture with them. Under “traditional” design practices, you’d
probably need to use URLs like these:

GET /products/new
GET /products/edit/1
POST /products/create
POST /products/update/1
POST /products/destroy/1

…and so on.

REST turns that on its head by deciding which controller action to
operate on by looking at what KIND of HTTP request it gets.

In a REST-based setup, the above may look something like this:

GET /products/new
GET /products/edit/1
POST /products/create
PUT /products/1
DELETE /products/1

In this case, a PUT method makes the controller know to use the update
method - you don’t pass it as part of your URL. Likewise, a DELETE
method makes the controller know to use the “destroy” action on the ID
of the object specified.

The advantage is that we can now think of things in terms of
resources, not “URL end points”. In this case, the resource in
question is “products”.

More information can be found here:
http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

This goes into some more detail about how to implement restful
routing.

As for making sure things are REST-based in your setup, you need the
following controller actions (automatically generated for you if you
generate via a scaffold):

  • index
  • new
  • create
  • edit
  • update
  • destroy

Each of them needs to operate on a single object at a time, either
creating one, returning it in the form, etc.

Then you need to set your routes up in routes.rb by specifying:

resources :products

(Or whatever it is - if it’s a message system and your controller is
called ‘messages_controller.rb’, that should read ‘resources:
messages’)

Good luck. The link I pasted above should be a really good start in
getting you on your way.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs