RESTful Rails. Part I

RESTful Rails have been a a much debated part of the Rails core, since the original restful_rails plugin was merged in to core just over a year ago. But with the improvements that have been made in Rails 2.0, they are here to stay, so it is important to understand what they are, how they work and when to (and when NOT to) use them. This is actually a pretty big topic, and I think it is worth while giving you some background first, so I’ll split the post in twain – Part I is theoretical, Part II is practical. So get your network protocol hats on, and get ready to learn about the inner workings of the language of the web.

REST vs. CRUD

Where would the web be with out acronyms? REST stands for REpresentational State Transfer and describes resources (in our case URLs) on which we can perform actions. CRUD, which stands for Create, Read, Update, Delete, are the actions that we perform. Although, in Rails, REST and CRUD are bestest buddies, the two can work fine on their own. In fact, every time you have written a backend system that allows you to add, edit and delete items from the database, and a frontend that allows you to view those items, you have been working with CRUD.

HTTP Verbs

Any web developer who has had to deal with form data would be aware of the GET and POST methods. The former will submit data back to the server via a query string that looks something like this ?key2=value2&key2=value2 and the latter sends the data via HTTP headers. What you might not realise is that every time you are load a page (that isn’t a form of type POST) you are making a GET request. So when you clicked the link in your RSS reader that bought you here, you made a GET request.

There are two other verbs that you might not have heard of because browsers don’t support them: PUT and DELETE. The delete action is obvious – if instructs the server to delete something. The PUT instruction is a little bit more cryptic – it is the same as POST action from the point of view that is sends data via headers, but it is designed to modify something (It also should create a new object if the object doesn’t already exist, but Rails ignores this minor technically).

The more astute of you may have noticed that GET sounds a lot like READ, POST looks like CREATE, PUT is the same as UPDATE and DELETE is DELETE. If you did, move straight to the head of the class – this is exactly the correlation that Rails uses when working with RESTful rails.

HTTP Response codes

Everyone knows that if you try to access a page that doesn’t exist, the server will throw you back 404 Page not Found, or if you mess up your rails code, you will get a 500 Application Error. These are response codes that the server sends back to let your browser know what is going on. Anything between 200-299 means the request was a success, 300-399 means the request was ok, but you need to do something else, 400-499 is an error and 500-599 is a really bad error. Just as there are verbs to match RESTful requests, there are response codes that correspond to RESTful responses.

200 OK
You will get from a GET, PUT or a DELETE request. It means that the request checked out and the appropriate action has been taken
201 Created
Notifies you that the POST command successfully created the posted object.
404 Not found
Means the request resource wasn’t found. You can get this from a GET, PUT or DELETE.
406 Not Acceptable
The verb isn’t allowed at the resources you reqested (More on this next part)
500 Internal Server Error
Something went horribly wrong

Resources

You would be used to seeing Rails URLs that look something like /posts/view/2 – which roughly translates to “Please let me view the post with the id number 2″. RESTful resources are very similar, in that they have a controller and (maybe) and id. What they generally don’t have is an action, because it is inherent in the HTTP verb.

To make this work, the Rails team have defined a number of special methods in the controllers that define resources – in this case posts (the .xml bit will become clear in the next part).

Method Resource Verb Explanation
index /posts.xml GET Returns all items
show /posts/1.xml GET Return a single item with id = 1
create /posts.xml POST Create an item
update /posts/1.xml PUT Update item with id = 1
delete /posts/1.xml DELETE Delete item with id = 1

In reality, you can define additional actions (You can’t make every web site fit this model), and I will go through these in my next post.

So, in the next part, we will look at how to create a RESTful rails project and how to wire everything up. Until then…

Replay

Category: programming Time: 2008-02-04 Views: 1
Tags:

Related post

  • Rapid RESTful Rails Apps -- No, Really! 2008-02-29

    As you may have read in my blog post, RESTful Rails Part I, RESTful Rails is a way of mapping HTTP verbs (GET, POST, PUT, and DELETE) to CRUD actions (Create, Read, Update, and Delete) in your Rails apps. If that sentence was complete gibberish to yo

  • RESTful Rails. Part II 2008-02-29

    For those of you who have been waiting with baited breath to read the second installment of my RESTful Rails blog post, wait no longer! Although it started as a blog post, it ended up being expanded to a full blown article entitled Rapid RESTful Rail

  • Rails Intro, Deep Dive: Installing Rails, Part One 2011-07-18

    Loccasions Rails Intro, Deep Dive: RVM Rails Intro, Deep Dive: Installing Rails, Part One Loccasions: Installing Rails Part 2 Rails Intro, Deep Dive: App Generation Rails Deep Dive: Application Setup, Loccasions Rails Deep Dive: Loccasions, Home Page

  • Loccasions: Installing Rails Part 2 2011-07-25

    Loccasions Rails Intro, Deep Dive: RVM Rails Intro, Deep Dive: Installing Rails, Part One Loccasions: Installing Rails Part 2 Rails Intro, Deep Dive: App Generation Rails Deep Dive: Application Setup, Loccasions Rails Deep Dive: Loccasions, Home Page

  • Techniques to Secure Your Website with Ruby On Rails (Part 1) 2012-06-08

    Techniques to Secure Your Website with Ruby on Rails Techniques to Secure Your Website with Ruby On Rails (Part 1) Techniques to Secure Your Website with Ruby On Rails (Part 2) Techniques to Secure Your Website with Ruby On Rails (Part 3) During the

  • RubySource: Rails Development 101: Installing Rails, Part One 2011-07-19

    Continuing from my last post about RVM , this time we'll install Rails. Again, this series attempts to give you a bit of a deeper dive into getting started with Rails, so we will go through some of the options available when first creating your world

  • RubySource: Rails Development 101: Installing Rails, Part II 2011-07-26

    Rails Gem Dependencies, Continued We left off with the TZInfo gem last time, and we'll start with ERubis in this article. Erubis is an implementation of eRuby, which is the Ruby expressions between the in Ruby views. ActionPack is kind of a big deal.

  • Techniques to Secure Your Website with Ruby On Rails (Part 2) 2012-06-18

    Techniques to Secure Your Website with Ruby on Rails Techniques to Secure Your Website with Ruby On Rails (Part 1) Techniques to Secure Your Website with Ruby On Rails (Part 2) Techniques to Secure Your Website with Ruby On Rails (Part 3) Last week w

  • Techniques to Secure Your Website with Ruby On Rails (Part 3) 2012-06-26

    Techniques to Secure Your Website with Ruby on Rails Techniques to Secure Your Website with Ruby On Rails (Part 1) Techniques to Secure Your Website with Ruby On Rails (Part 2) Techniques to Secure Your Website with Ruby On Rails (Part 3) This is the

  • Webserver flow : Using apex Rest API part of a managed package installed in client orgs 2014-04-21

    I am building a beta managed package in which I created a connected app ( which generated its own clientsecret and clientid in my dev org). Now when someone install this package in their org they all will get a copy of my class ( having rest api) let

  • PHP connector for communicating with a REST API (Part 1) 2015-05-26

    I wrote a PHP connector that will allow me to communicate with a REST API. This is the first time I am trying to make my code as clean as possible. I want to help myself learning how to better code. I have done everything I can think of to make my co

  • PHP connector for communicating with a REST API (part 2) 2015-05-30

    I wrote a PHP connector that will allow me to communicate with a REST API using PHP. The class was little over 1500 lines of code, it was difficult to manage it. It was a nightmare to even look at it as you can probably imagine. My intention was to b

  • Spring REST drops part of url path 2016-02-01

    we are using Spring 4, Spring Security 4 and Spring remoting with Primefaces (JSF). Wanting to replace the Spring remoting stuff with Spring REST. Have followed examples and have got it working but only after removing part of the incoming URL so Spri

  • Rails Deep Dive: Loccasions, Spork, Events and Authorization 2011-09-26

    Loccasions Rails Intro, Deep Dive: RVM Rails Intro, Deep Dive: Installing Rails, Part One Loccasions: Installing Rails Part 2 Rails Intro, Deep Dive: App Generation Rails Deep Dive: Application Setup, Loccasions Rails Deep Dive: Loccasions, Home Page

  • Rails Deep Dive: Loccasions, Making Events 2011-10-07

    Loccasions Rails Intro, Deep Dive: RVM Rails Intro, Deep Dive: Installing Rails, Part One Loccasions: Installing Rails Part 2 Rails Intro, Deep Dive: App Generation Rails Deep Dive: Application Setup, Loccasions Rails Deep Dive: Loccasions, Home Page

  • 6 things to try in Rails this year 2008-01-09

    It seems that blog posts in the first couple of weeks of the new year (happy new year by the way) follow the "x things to do this year" meme as a virtual homage to new years resolutions. Never one to buck a trend, I have prepared this short list

  • Outer rails for a Dell PowerEdge 1950 2010-10-20

    I'm looking at buying a Dell PowerEdge 1950 on eBay. It comes with rails (HN185 and GU092) but as best I can tell these are only the inner rails. Does anyone know which outer rails these go to? Thanks. --------------Solutions------------- Racksolutio

  • Rails Intro, Deep Dive: RVM 2011-06-30

    Loccasions Rails Intro, Deep Dive: RVM Rails Intro, Deep Dive: Installing Rails, Part One Loccasions: Installing Rails Part 2 Rails Intro, Deep Dive: App Generation Rails Deep Dive: Application Setup, Loccasions Rails Deep Dive: Loccasions, Home Page

  • Rails Intro, Deep Dive: App Generation 2011-08-02

    Loccasions Rails Intro, Deep Dive: RVM Rails Intro, Deep Dive: Installing Rails, Part One Loccasions: Installing Rails Part 2 Rails Intro, Deep Dive: App Generation Rails Deep Dive: Application Setup, Loccasions Rails Deep Dive: Loccasions, Home Page

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.983 (s). 13 q(s)