<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3939706329553834000</id><updated>2012-02-15T23:36:46.994-08:00</updated><category term='REST HTTP Routes'/><category term='REST HTTP Controller'/><category term='REST HTTP verb'/><category term='Blog Syntax Highlighting'/><title type='text'>TNT on Rails</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tnt-on-rails.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3939706329553834000/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://tnt-on-rails.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>tobago</name><uri>http://www.blogger.com/profile/10561958786057528168</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://4.bp.blogspot.com/_kv7KERtXJdc/SXo5bPxTILI/AAAAAAAAABE/mayjxroLpQk/S220/avatar.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3939706329553834000.post-6536554206956004551</id><published>2009-01-23T22:38:00.001-08:00</published><updated>2009-02-01T01:20:54.489-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='REST HTTP Routes'/><title type='text'>REST In Piece</title><content type='html'>&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3939706329553834000-6536554206956004551?l=tnt-on-rails.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tnt-on-rails.blogspot.com/feeds/6536554206956004551/comments/default' title='Kommentare zum Post'/><link rel='replies' type='text/html' href='http://tnt-on-rails.blogspot.com/2009/01/rest-in-piece.html#comment-form' title='0 Kommentare'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3939706329553834000/posts/default/6536554206956004551'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3939706329553834000/posts/default/6536554206956004551'/><link rel='alternate' type='text/html' href='http://tnt-on-rails.blogspot.com/2009/01/rest-in-piece.html' title='REST In Piece'/><author><name>tobago</name><uri>http://www.blogger.com/profile/10561958786057528168</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://4.bp.blogspot.com/_kv7KERtXJdc/SXo5bPxTILI/AAAAAAAAABE/mayjxroLpQk/S220/avatar.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3939706329553834000.post-5289881917716540090</id><published>2009-01-23T22:37:00.000-08:00</published><updated>2009-02-01T01:21:13.184-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='REST HTTP Controller'/><title type='text'>The Very BEST Of REST (The Praxis)</title><content type='html'>&lt;p&gt;Since you know the abstract of REST (otherwise read &lt;a href="http://tnt-on-rails.blogspot.com/2009/01/take-rest.html"&gt;"Take A REST"&lt;/a&gt; first), we'll start to drive the train to REST. Fasten your seat belts!&lt;/p&gt;
&lt;p&gt;I will use a simple sample to demonstrate the way REST works in Rails. We will talk about a person/ people to manipulate. As I mentioned in the previous article the Controller maps the resource/ object of class ActiveRecord::Base.&lt;/p&gt;
&lt;p&gt;I assume you have a database running. I skip that part, because it depends on your preferences which DMS you use. I don't care if it's a sqlite, mysql or whatever. I use postgres.&lt;/p&gt;

&lt;h3&gt;All In One (Line)&lt;/h3&gt;

As you already know:
&lt;pre class="console"&gt;
~/blogspot$ rails train
&lt;/pre&gt;
creates a rails application named train. And who goes by train? Correctly, it's all about people:
&lt;pre class="console"&gt;
~/blogspot$ script/generate scaffold person name:string surname:string age:integer
&lt;/pre&gt;
Our People have a name and a surname the same as an age. This one liner creates the model Person in the a app/models/person.rb:
&lt;a href="#" name="person"&gt;&lt;/a&gt;
&lt;pre name="code" class="ruby"&gt;
class Person &amp;lt; ActiveRecord::Base
end
&lt;/pre&gt;
Don't forget to
&lt;pre class="console"&gt;
~/blogspot$ rake db:migrate
&lt;/pre&gt;
to have the appropriate table in the database after you set up your config/database.yml (you know how).
The extra benefit is the line
&lt;a href="#" name="routes"&gt;&lt;/a&gt;
&lt;pre name="code" class="ruby"&gt;
map.resources :people
&lt;/pre&gt;
in the config/routes.rb to create the routes and their URL helper. I will touch it later on once again.
But that's not all. The extra extra benefit is the app/controllers/people_controller.rb (I compressed the comments a bit; you can compare with your own generated):
&lt;h3&gt;The Controller&lt;/h3&gt;
&lt;a href="#" name="controller"&gt;&lt;/a&gt;
&lt;pre name="code" class="ruby"&gt;
class PeopleController &lt; ApplicationController
  def index
    @people = Person.find(:all)
    respond_to do |format|
      format.html # GET /people --&gt; index.html.erb
      format.xml{render :xml =&gt; @people} # GET /people.xml
    end
  end

  def show
    @person = Person.find(params[:id])
    respond_to do |format|
      format.html # GET /people/1 --&gt; show.html.erb
      format.xml{render :xml =&gt; @person} # GET /people/1.xml
    end
  end

  def new
    @person = Person.new
    respond_to do |format|
      format.html # GET /people/new --&gt; new.html.erb
      format.xml{render :xml =&gt; @person} # GET /people/new
    end
  end

  def edit
    @person = Person.find(params[:id]) # GET /people/1/edit
  end
  
  def create
    @person = Person.new(params[:person])
    respond_to do |format|
      if @person.save
        flash[:notice] = 'Person was successfully created.'
        format.html{redirect_to(@person)} # POST /people
        format.xml{render :xml =&gt; @person, :status =&gt; :created, :location =&gt; @person} # POST /people.xml
      else
        format.html{render :action =&gt; "new"} # failed validation
        format.xml  { render :xml =&gt; @person.errors, :status =&gt; :unprocessable_entity }
      end
    end
  end

  def update
    @person = Person.find(params[:id])
    respond_to do |format|
      if @person.update_attributes(params[:person])
        flash[:notice] = 'Person was successfully updated.'
        format.html{redirect_to(@person)} # PUT /people/1
        format.xml{head :ok} # PUT /people/1.xml
      else
        format.html{render :action =&gt; "edit"} # failed validation
        format.xml{render :xml =&gt; @person.errors, :status =&gt; :unprocessable_entity}
      end
    end
  end

  def destroy
    @person = Person.find(params[:id])
    @person.destroy
    respond_to do |format|
      format.html{redirect_to(people_url)} # DELETE /people/1
      format.xml{head :ok} # DELETE /people/1.xml
    end
  end
end
&lt;/pre&gt;
&lt;p&gt;Gosh! Can you believe that one single script/generate does such an incredible job for you? It definitely takes more time to explain all these lines and what it means for you than typing it. Not to mention how long it would take these lines. It's unbelievable but that's not all yet... &lt;/p&gt;
The extra extra extra benefit are some new templates generated in app/views/people:
&lt;h3&gt;The Templates&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;index.html.erb&lt;/li&gt;
&lt;li&gt;show.html.erb&lt;/li&gt;
&lt;li&gt;edit.html.erb&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These templates are for rendering the actions "index", "show" and "edit".&lt;/p&gt;
The template app/views/people/index.html.erb:
&lt;a href="#" name="index"&gt;&lt;/a&gt;
&lt;pre name="code" class="xml"&gt;
&lt;h1&gt;Listing people&lt;/h1&gt;
&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;Name&lt;/th&gt;
    &lt;th&gt;Surname&lt;/th&gt;
    &lt;th&gt;Age&lt;/th&gt;
  &lt;/tr&gt;
&lt;% for person in @people %&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;%=h person.name %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%=h person.surname %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%=h person.age %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%= link_to 'Show', person %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%= link_to 'Edit', edit_person_path(person) %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%= link_to 'Destroy', person, :confirm =&gt; 'Are you sure?', :method =&gt; :delete %&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;% end %&gt;
&lt;/table&gt;
&lt;br /&gt;
&lt;%= link_to 'New person', new_person_path %&gt;
&lt;/pre&gt;
&lt;p&gt;Please be patient. I will go through it in a moment.&lt;/p&gt;
The template app/views/people/show.html.erb:
&lt;a href="#" name="show"&gt;&lt;/a&gt;
&lt;pre name="code" class="xml"&gt;
&lt;p&gt;
  &lt;b&gt;Name:&lt;/b&gt;
  &lt;%=h @person.name %&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;b&gt;Surname:&lt;/b&gt;
  &lt;%=h @person.surname %&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;b&gt;Age:&lt;/b&gt;
  &lt;%=h @person.age %&gt;
&lt;/p&gt;
&lt;%= link_to 'Edit', edit_person_path(@person) %&gt; |
&lt;%= link_to 'Back', people_path %&gt;
&lt;/pre&gt;
The template app/views/people/edit.html.erb:
&lt;a href="#" name="edit"&gt;&lt;/a&gt;
&lt;pre name="code" class="xml"&gt;
&lt;h1&gt;Editing person&lt;/h1&gt;
&lt;% form_for(@person) do |f| %&gt;
  &lt;%= f.error_messages %&gt;
  &lt;p&gt;
    &lt;%= f.label :name %&gt;&lt;br /&gt;
    &lt;%= f.text_field :name %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.label :surname %&gt;&lt;br /&gt;
    &lt;%= f.text_field :surname %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.label :age %&gt;&lt;br /&gt;
    &lt;%= f.text_field :age %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.submit "Update" %&gt;
  &lt;/p&gt;
&lt;% end %&gt;
&lt;%= link_to 'Show', @person %&gt; |
&lt;%= link_to 'Back', people_path %&gt;
&lt;/pre&gt;
And at least the new.html.erb:
&lt;a href="#" name="new"&gt;&lt;/a&gt;
&lt;pre name="code" class="xml"&gt;
&lt;h1&gt;New person&lt;/h1&gt;
&lt;% form_for(@person) do |f| %&gt;
  &lt;%= f.error_messages %&gt;
  &lt;p&gt;
    &lt;%= f.label :name %&gt;&lt;br /&gt;
    &lt;%= f.text_field :name %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.label :surname %&gt;&lt;br /&gt;
    &lt;%= f.text_field :surname %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.label :age %&gt;&lt;br /&gt;
    &lt;%= f.text_field :age %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.submit "Create" %&gt;
  &lt;/p&gt;
&lt;% end %&gt;
&lt;%= link_to 'Back', people_path %&gt;
&lt;/pre&gt;
&lt;p&gt;
... man, the Rails train is an incredible fast thingie (The Concorde wouldn't be faster, if it still flew). And it does all for you, including RESTful routes. This standardized controller and its templates handle the CRUD of every person in the train.&lt;/p&gt;
&lt;p&gt;Right now the more tedious job to explain about what's going on is waiting for the poor author (me).&lt;/p&gt;

&lt;h3&gt;REST URLs vs. Granny URLs&lt;/h3&gt;

Let's start with the created &lt;a href="#controller"&gt;PeopleController&lt;/a&gt;. It contains the seven actions to process the people of the resource class Person (model). The list below abstracts them in short manner.
&lt;a href="#" name="urls"&gt;&lt;/a&gt;
&lt;table class="list"&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Action&lt;/th&gt;
      &lt;th&gt;HTTP&lt;/th&gt;
      &lt;th&gt;Route helper&lt;/th&gt;
      &lt;th&gt;URL&lt;/th&gt;
      &lt;th&gt;Response&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr class="odd"&gt;
      &lt;td&gt;index&lt;/td&gt;
      &lt;td&gt;GET&lt;/td&gt;
      &lt;td&gt;&lt;i&gt;people&lt;/i&gt;_path&lt;/td&gt;
      &lt;td&gt;/people&lt;/td&gt;
      &lt;td&gt;Collection of all people&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="even"&gt;
      &lt;td&gt;show&lt;/td&gt;
      &lt;td&gt;GET&lt;/td&gt;
      &lt;td&gt;&lt;i&gt;person&lt;/i&gt;_path(@person.id)&lt;/td&gt;
      &lt;td&gt;/people/1&lt;/td&gt;
      &lt;td&gt;Person with id 1 (to show)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="odd"&gt;
      &lt;td&gt;new&lt;/td&gt;
      &lt;td&gt;GET&lt;/td&gt;
      &lt;td&gt;new_&lt;i&gt;person&lt;/i&gt;_path&lt;/td&gt;
      &lt;td&gt;/people/new&lt;/td&gt;
      &lt;td&gt;New Person with nil attributes for creation&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="even"&gt;
      &lt;td&gt;edit&lt;/td&gt;
      &lt;td&gt;GET&lt;/td&gt;
      &lt;td&gt;edit_&lt;i&gt;person&lt;/i&gt;_path(@person.id)&lt;/td&gt;
      &lt;td&gt;/people/1/edit&lt;/td&gt;
      &lt;td&gt;Person with id 1 for update&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="odd"&gt;
      &lt;td&gt;create&lt;/td&gt;
      &lt;td&gt;POST&lt;/td&gt;
      &lt;td&gt;&lt;i&gt;people&lt;/i&gt;_path&lt;/td&gt;
      &lt;td&gt;/people&lt;/td&gt;
      &lt;td&gt;Person with new created id and attributes (sent by form)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="even"&gt;
      &lt;td&gt;update&lt;/td&gt;
      &lt;td&gt;PUT&lt;/td&gt;
      &lt;td&gt;&lt;i&gt;person&lt;/i&gt;_path(@person.id)&lt;/td&gt;
      &lt;td&gt;/people/1&lt;/td&gt;
      &lt;td&gt;Person with updated attributes (sent by form)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="odd"&gt;
      &lt;td&gt;destroy&lt;/td&gt;
      &lt;td&gt;DELETE&lt;/td&gt;
      &lt;td&gt;&lt;i&gt;person&lt;/i&gt;_path(@person.id)&lt;/td&gt;
      &lt;td&gt;/people/1&lt;/td&gt;
      &lt;td&gt;Collection of all people except the person with id&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
The route helpers are generated by the &lt;a href="#routes"&gt;routes.rb&lt;/a&gt; dynamically. You can ask for them by
&lt;pre class="console"&gt;
~/blogspot$ rake routes
&lt;/pre&gt;
Comparing the RESTful routes and the old fashioned way:
&lt;table class="list"&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Action&lt;/th&gt;
      &lt;th&gt;RESTful URL&lt;/th&gt;
      &lt;th&gt;old fashioned URL&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr class="odd"&gt;
      &lt;td&gt;index&lt;/td&gt;
      &lt;td&gt;/people&lt;/td&gt;
      &lt;td&gt;/people/index&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="even"&gt;
      &lt;td&gt;show&lt;/td&gt;
      &lt;td&gt;/people/1&lt;/td&gt;
      &lt;td&gt;/people/show/1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="odd"&gt;
      &lt;td&gt;new&lt;/td&gt;
      &lt;td&gt;/people/new&lt;/td&gt;
      &lt;td&gt;/people/new&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="even"&gt;
      &lt;td&gt;edit&lt;/td&gt;
      &lt;td&gt;/people/1/edit&lt;/td&gt;
      &lt;td&gt;/people/edit/1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="odd"&gt;
      &lt;td&gt;create&lt;/td&gt;
      &lt;td&gt;/people&lt;/td&gt;
      &lt;td&gt;/people/create&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="even"&gt;
      &lt;td&gt;update&lt;/td&gt;
      &lt;td&gt;/people/1&lt;/td&gt;
      &lt;td&gt;/people/update/1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="odd"&gt;
      &lt;td&gt;destroy&lt;/td&gt;
      &lt;td&gt;/people/1&lt;/td&gt;
      &lt;td&gt;/people/destroy/1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Do you see the differences? I mean, do you realize the sweetness of the REST URLs? The URL doesn't declare anything about the action. They are stripped to their resource they represent. Who knows what to do with the resource (URL) "/people/1"? Show, update or destroy it? Nobody knows. The HTTP verb declares it. And that's how it should be. No resource-action mixup anymore, anytime!&lt;/p&gt;

&lt;h3&gt;The Special Requests&lt;/h3&gt;

&lt;p&gt;Testing the application creates logs according to the requests.&lt;p/&gt;
Show request:
&lt;pre class="console"&gt;
Processing PeopleController#show (for 127.0.0.1 at 2009-01-26 19:40:52) [GET]
Parameters: {"action"=&gt;"show", "id"=&gt;"1", "controller"=&gt;"people"}
&lt;/pre&gt;
Update request:
&lt;pre class="console"&gt;
Processing PeopleController#update (for 127.0.0.1 at 2009-01-26 19:41:01) [PUT]
Parameters: {"commit"=&gt;"Update", "person"=&gt;{"name"=&gt;"Destra", "surname"=&gt;"Garcia", "age"=&gt;"29"},
"authenticity_token"=&gt;"87b9c2baa4e1114ca0b97384253ac6c283f19cee", "_method"=&gt;"put", 
action"=&gt;"update", "id"=&gt;"1", "controller"=&gt;"people"}
&lt;/pre&gt;
&lt;p&gt;Please survey the parameters hash sent by the form. First it contains the data of person called Destra Garcia (btw. the best female soca artist ever). But more interesting is the "_method"=&gt;"put", which is responsible for pointing to update action instead of show (remember the equal URL). Who caused it? I will mention it, when I go into detail of the edit.html.erb template. For now it's sufficent to just know it.&lt;/p&gt;
Destroy request:
&lt;pre class="console"&gt;
Processing PeopleController#destroy (for 127.0.0.1 at 2009-01-26 20:07:57) [DELETE]
Parameters: {"authenticity_token"=&gt;"87b9c2baa4e1114ca0b97384253ac6c283f19cee",
"_method"=&gt;"delete", "action"=&gt;"destroy", "id"=&gt;"1", "controller"=&gt;"people"}
&lt;/pre&gt;
&lt;p&gt;Once again the "_method" parameter with its value "destroy" this time. The "authenticity_token" helps to prevent XSRF server side and is part of all create, update and destroy requests.&lt;/p&gt;
Now let's take a deeper look into the sent headers with the help of curl (nice tool to send/ receive data to/ from a server using amongst others HTTP).
&lt;pre class="console"&gt;
~/blogspot$ curl -H "Accept: text/html" -i -X GET http://localhost:3000/people/1
&lt;/pre&gt;
It returns the header and the HTML response (I omitted the uninteresting stuff):
&lt;pre class="console"&gt;
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
&lt;/pre&gt;
So the show action responded to the HTML request. It also does when requesting XML:
&lt;pre class="console"&gt;
~/blogspot$ curl -H "Accept: application/xml" -i -X GET http://localhost:3000/people/1
&lt;/pre&gt;
The response:
&lt;pre class="console"&gt;
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
&lt;/pre&gt;
&lt;p&gt;succeed with the requested resource in a XML styleee. You also can test it in your browser with URL localhost:3000/people/1.xml (assumed that you run your server on port 3000 and there is a resource people/1).
Why does the server can respond to different requests using the same action? Sugar, sugar, Rails. Yep! Rails evaluates the header and returns the appropriate resource in the requested mode. HTML? XML (Webservice via ActiveResource gives a broad hint)? No problem. You will use the same action and you're done. Lucky guy.&lt;/p&gt;
&lt;p&gt;It's time to render the response, isn't it? By default rails searches for the HTML template to render (when there is no respond_to call). Since 2.0 we use ERB for rendering HTML. The files are marked with the extension html.erb; remember the automatically generated templates when I used scaffold to generate the person resource.&lt;/p&gt;

&lt;h3&gt;The Links In Detail&lt;/h3&gt;

&lt;p&gt;Now let's elaborate to the &lt;a href="#index"&gt;index.html.erb&lt;/a&gt;, just to pick up the first one.&lt;/p&gt;
&lt;p&gt;It renders the collection of people. Basically it contains a table with the people our table can deliver. As by former scaffolding, there are also the three links to work the single entity. Show, Edit and Delete, just to name it. "Show" shows the entity in detail. The URL is generated by assigning the object itself. Rails inspects the object and assumes the controller by its class (a controller represents a resource!). This way to generate the URL is a polymorphic one. But that doesn't matter at this point. I just wanted to mention it, because I explain it in a later article ;) . The URL "/people/1" will call the &lt;a href="#urls"&gt;show&lt;/a&gt; action of PeopleController. You also could have used the explicit version "person_path(person)" instead of just assigning simply the object. The next link for calling the edit action named "Edit" is an example of an explicit URL helper: edit_person_path(person). It's one of the dynamically generated helper. Rails simply extracts the id of the passed object. So the call also could look like edit_people_path(person.id). You better save your energy for typing the longer version and watch the last link ("Destroy") in the line. As you already know, the HTTP verb "delete" needs to be sent to call the destroy action. The browser is unable to do it, so let Rails do the job. I set the :method option of the link_to helper to :delete. So Rails realizes that it has to generate a form. For obvious reasons it is not a HTML form. Right now it's sufficient to know that the link establish a form on click:&lt;/p&gt;
&lt;pre name="code" class="xml"&gt;
&lt;a onclick="if (confirm('Are you sure?')) { var f = document.createElement('form');
f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; 
f.action = this.href;var m = document.createElement('input'); 
m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); 
m.setAttribute('value', 'delete'); f.appendChild(m);
var s = document.createElement('input'); s.setAttribute('type', 'hidden'); 
s.setAttribute('name', 'authenticity_token'); 
s.setAttribute('value', '87b9c2baa4e1114ca0b97384253ac6c283f19cee'); 
f.appendChild(s);f.submit(); };return false;" href="/people/2"&gt;Destroy&lt;/a&gt;
&lt;/pre&gt;
&lt;p&gt;Before sending the form you will have to confirm, if you really are sure to destroy the item. If so, the form will be sent with the already mentioned attributes, "_method" =&gt; "delete" amongst others. Please compare the href attribute of the "Destroy" link with the "Show" href.
&lt;/p&gt;
&lt;p&gt;
The "New person" link is described briefly. It uses the explicit helper new_person_path to generate "/people/new".
&lt;/p&gt;
&lt;p&gt;The &lt;a href="#show"&gt;show.html.erb&lt;/a&gt; template contains two links. One to "Edit" the chosen item and another one ("Back") to return to the complete collection. I already declared the "Edit" link before. And to return to the people is pretty easy. The URL helper people_path does it.&lt;/p&gt;
&lt;p&gt;O.K. the next template (&lt;a href="#edit"&gt;edit.html.erb&lt;/a&gt;) is more interesting, I swear. I suppose I don't need to describe the both links "Show" and "Back" once again. So I start with form. It is generated by the form_for helper. And by the way I advise you to make extensive use of it. It's a sweet one, believe me. It expects an object and instantiates a form object of it, you conveniently can use to access the attributes to fill the form input and select fields as I describe in another article. The HTML of the form looks like:
&lt;pre name="code" class="xml"&gt;
&lt;form id="edit_person_3" class="edit_person" method="post" action="/people/3"&gt;
&lt;div style="margin: 0pt; padding: 0pt;"&gt;
&lt;input type="hidden" value="put" name="_method"/&gt;
&lt;input type="hidden" value="87b9c2baa4e1114ca0b97384253ac6c283f19cee" name="authenticity_token"/&gt;
&lt;/div&gt;
// ...
&lt;/form&gt;
&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;You're right. The URL is equal to the one calling the show action ("Show"). And it does POST as every form should. But it also sends a parameter called "_method" with the value "put", affiliated by a hidden field. And that reveals the request to be PUT (update action). The same trick used for calling the destroy action&lt;/p&gt;
&lt;p&gt;The form of the &lt;a href="#new"&gt;new.html.erb&lt;/a&gt; doesn't need any trick. It just uses the form's POST to be POST:
&lt;pre name="code" class="xml"&gt;
&lt;form id="new_person" class="new_person" method="post" action="/people"&gt;
&lt;div style="margin: 0pt; padding: 0pt;"&gt;
&lt;input type="hidden" value="1fa44682a8700d2ab0af9a241e88b4e8595f1b3d" name="authenticity_token"/&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/pre&gt;
Here you also can admire the fancy form_for helper. I really appreciate it.&lt;/p&gt;
&lt;p&gt;Oh lord, to write these lines took a lot more time than creating the life cycle of the person resource itself. I hope you enjoyed it. So do yourself a favour and take a REST and relax.
&lt;/p&gt;
&lt;strong class="red"&gt;Nice up your App!&lt;/strong&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3939706329553834000-5289881917716540090?l=tnt-on-rails.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tnt-on-rails.blogspot.com/feeds/5289881917716540090/comments/default' title='Kommentare zum Post'/><link rel='replies' type='text/html' href='http://tnt-on-rails.blogspot.com/2009/01/very-best-of-rest.html#comment-form' title='0 Kommentare'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3939706329553834000/posts/default/5289881917716540090'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3939706329553834000/posts/default/5289881917716540090'/><link rel='alternate' type='text/html' href='http://tnt-on-rails.blogspot.com/2009/01/very-best-of-rest.html' title='The Very BEST Of REST (The Praxis)'/><author><name>tobago</name><uri>http://www.blogger.com/profile/10561958786057528168</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://4.bp.blogspot.com/_kv7KERtXJdc/SXo5bPxTILI/AAAAAAAAABE/mayjxroLpQk/S220/avatar.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3939706329553834000.post-8459035957684309624</id><published>2009-01-23T22:36:00.000-08:00</published><updated>2009-01-25T01:27:35.258-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='REST HTTP verb'/><title type='text'>Take A REST (The Theory)</title><content type='html'>&lt;p&gt;Since Rails 1.2 there was a chat about REST and what it means for us (at the end of the food chain). Before giving some practical examples on how to use it in Rails in a later paper I'd like to write some notes about its theory.&lt;/p&gt;
&lt;p&gt;Once upon a time ...&lt;/p&gt;
No. Seriously. In 2000 Roy Fielding launched REST in a &lt;a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm"&gt;dissertation&lt;/a&gt;. REST stands for Representational State Transfer, which naming includes all about what's going behind the curtain. I try to define each single word in a short briefing in hope that nobody will hurt me for its oversimplification:
&lt;ul&gt;
&lt;li&gt;&lt;span class="red"&gt;Representational&lt;/span&gt; stands for: Every resource is represented by an own unique URI (so it is a mapping URI == resource/data/HTML/object/PDF/whatever_you_want_it_to_be)&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;State&lt;/span&gt; means to have some HTTP verbs, which job is to declare what to do with this resource (I will describe these verbs later)&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;Transfer&lt;/span&gt; is self-explanatory (isn't it?)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Please let me emphasize the Representation thingie once again: an &lt;span class="red"&gt;URI represents a RESOURCE&lt;/span&gt;. It's not that I like wagging forefingers, it's because of the later understanding why Rails generates such routes.&lt;/p&gt;
O.K. now I will name these HTTP "verbs" (as I promised; and I always keep my promises ;) )
In the orignal HTTP specification there a declared the following request methods:
&lt;ol&gt;
&lt;li&gt;&lt;span class="red"&gt;GET:&lt;/span&gt; get a resource&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;POST:&lt;/span&gt; update a resource&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;PUT:&lt;/span&gt; create a resource&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;DELETE:&lt;/span&gt; delete a resource&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;HEAD:&lt;/span&gt; returns metadata of a resource&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;TRACE:&lt;/span&gt; returns the original query the server received (e.g to compare, if the query was fiddled intermediately)&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;OPTIONS:&lt;/span&gt; returns a list of features the server supports&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;CONNECT:&lt;/span&gt; for ssl tunnel&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first two should be known very well. That is why I don't go into detail how a get or post request looks like. Apart from that Wiki does.&lt;/p&gt;
&lt;p&gt;But reading the list carefully you certainly noticed that the leading four verbs map the CRUD (Create/ Read/ Update/ Delete) actions needed to proceed the full life circle an object is going through (maybe you knew before, but if so why the hell are you still reading this article?). Anyway, the Rails guys catched these four verbs to implement REST in RoR ('R' words are great!). So they had to reactivate PUT and DELETE, though we all know that the common browser don't know them (in situations like this I always remember the words of Bill Gates a browser was just a little snippet of code...)&lt;/p&gt;
&lt;p&gt;... sorry for drifting away. The Rails community solved it by sending (posting) the verbs as parameters as I describe in the article &lt;a href="http://tnt-on-rails.blogspot.com/2009/01/very-best-of-rest.html"&gt;"The very BEST of REST"&lt;/a&gt;. A short answer as long as IE &amp; Co. keep on being dumbly. To put it in a nutshell, a RESTful Rails application uses HTTP verbs "GET", "POST", "PUT" and "DELETE" to get, create, update and delete resources (objects) represented by the URI.&lt;/p&gt;
&lt;p&gt;Believe it or not, that's all REST is about.&lt;/p&gt;
&lt;p&gt;Now let's come to terms.&lt;/p&gt;
The pros:
&lt;ul&gt;
&lt;li&gt;&lt;span class="red"&gt;Clean URLs&lt;/span&gt; representing only the resource itself without any actions&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;Different Responses&lt;/span&gt; of the same action are possible depending on the request (HTML, Ajax, XML).&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;Less code&lt;/span&gt; through prevention of code repetitions (DRY) by using the same action for different requests (Multiclient).&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;CRUD based Controller&lt;/span&gt; represent a specific kind of resource (simplifies and cleans up the structure of controllers).&lt;/li&gt;
&lt;li&gt;&lt;span class="red"&gt;Clean Controllers&lt;/span&gt; are easier and enjoying to maintain&lt;/li&gt;
&lt;/ul&gt;

The contras:
&lt;ul&gt;
&lt;li&gt;You have to get familiar with a new technique&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Sounds like an easy decision to make. So make it and pimp your Rails application with REST.&lt;/p&gt;
&lt;p&gt;I still didn't characterize how? True dat, mon!&lt;/p&gt;
&lt;strong class="red"&gt;Read &lt;a href="http://tnt-on-rails.blogspot.com/2009/01/very-best-of-rest.html"&gt;The very BEST of REST&lt;/a&gt;!!&lt;/strong&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3939706329553834000-8459035957684309624?l=tnt-on-rails.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tnt-on-rails.blogspot.com/feeds/8459035957684309624/comments/default' title='Kommentare zum Post'/><link rel='replies' type='text/html' href='http://tnt-on-rails.blogspot.com/2009/01/take-rest.html#comment-form' title='0 Kommentare'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3939706329553834000/posts/default/8459035957684309624'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3939706329553834000/posts/default/8459035957684309624'/><link rel='alternate' type='text/html' href='http://tnt-on-rails.blogspot.com/2009/01/take-rest.html' title='Take A REST (The Theory)'/><author><name>tobago</name><uri>http://www.blogger.com/profile/10561958786057528168</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://4.bp.blogspot.com/_kv7KERtXJdc/SXo5bPxTILI/AAAAAAAAABE/mayjxroLpQk/S220/avatar.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3939706329553834000.post-1502483539727707913</id><published>2009-01-21T08:51:00.000-08:00</published><updated>2009-01-23T22:36:40.602-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Blog Syntax Highlighting'/><title type='text'>How To Blog Ruby</title><content type='html'>&lt;p&gt;My wish to blog about some Ruby'n'Rails stuff is coming true and Boom! there was the first issue. How to hightlight the syntax of the ruby code snippets? Gosh!
But fortunately there is the &lt;a href="http://code.google.com/p/syntaxhighlighter/"&gt;SyntaxHighlighter&lt;/a&gt; :) based on CSS and JavaScript. Now i describe how I got it working.&lt;/p&gt;
I promise you, it's done in 3 steps.

&lt;h3&gt;Step 1&lt;/h3&gt;
&lt;p&gt;Download the SyntaxHighlighter (just follow the link above) and you should receive a lot of files.&lt;/p&gt;
&lt;p&gt;The shCore.js and SyntaxHighlighter.css are essentially. The first one makes the code looking as the second file wants it to look like: Bling Bling.&lt;/p&gt;
&lt;p&gt;The rest of JS pack are shBrushXXX.js files amongst others the shBrushRuby.js. Now you have to park them somewhere in the world wild web. I abused &lt;a href="http://gecities.com/"&gt;Geocities&lt;/a&gt; for it but &lt;a href="http://sites.google.com/"&gt;Google's Sites&lt;/a&gt; should do the job the same. It doesn't matter. The main point is that they are available. Otherwise no Bling Bling.&lt;/p&gt;
To be able to highlight more kind of code like SQL, CSS, JScript or XML I uploaded the appropriate shBrushSql.js, shBrushCss.js, shBrushJScript.js and shBrushXml.js too.

&lt;h3&gt;Step 2&lt;/h3&gt;
&lt;p&gt;Before getting the code snippets sparkling you better should include the needed files inside the head tags as I did:

&lt;pre name="code" class="xml"&gt;
&amp;lt;link type="text/css" rel="stylesheet" href="http://your_server/path/to/SyntaxHighlighter.css"&amp;gt;&lt;/link&amp;gt;
&amp;lt;script src="http://your_server/path/to/shCore.js" language="javascript"&amp;gt;&amp;lt;/script&amp;gt;&lt;/br&gt;
&amp;lt;script src="http://your_server/path/to/shBrushRuby.js" language="javascript"&amp;gt;&amp;lt;/script&amp;gt;&lt;/br&gt;
&amp;lt;script src="http://your_server/path/to/shBrushSql.js" language="javascript"&amp;gt;&amp;lt;/script&amp;gt;&lt;/br&gt;
&amp;lt;script src="http://your_server/path/to/shBrushCss.js" language="javascript"&amp;gt;&amp;lt;/script&amp;gt;&lt;/br&gt;
&amp;lt;script src="http://your_server/path/to/shBrushJScript.js" language="javascript"&amp;gt;&amp;lt;/script&amp;gt;&lt;/br&gt;
&amp;lt;script src="http://your_server/path/to/shBrushXml.js" language="javascript"&amp;gt;&amp;lt;/script&amp;gt;&lt;/br&gt;
&lt;/pre&gt;
and before the closing body tag &lt;/body&gt;:
&lt;pre name="code" class="xml"&gt;
&amp;lt;script language='javascript'&amp;gt;
dp.SyntaxHighlighter.ClipboardSwf = 'http://your_server/path/to/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('code');
&amp;lt;/script&amp;gt;
&lt;/pre&gt;
Don't miss it. It's a must have.&lt;/p&gt;

Did you notice something? Yeah man, it's the first piece of code glitter. Calm down, the next bit is coming.
Let's make the blog talk ruby in ...

... &lt;h3&gt;Step 3&lt;/h3&gt;
&lt;p&gt;O.K. ruby is the first choice of talking and so we start our very first ruby snippet rrrrright now. Man, how exciting!
And it's that easy.&lt;/p&gt;
&lt;p&gt;Use the &lt;span class="red"&gt;&amp;lt;pre&amp;gt;&lt;/span&gt; tag for wrapping the code and set the &lt;span class="red"&gt;name&lt;/span&gt; attribute to &lt;span class="red"&gt;code&lt;/span&gt; and the &lt;span class="red"&gt;class&lt;/span&gt; attribute to whatever you want to look like, for example:
&lt;pre name="code" class="xml"&gt;
&amp;lt;pre name="code" class="ruby"&amp;gt;
  class Person &amp;lt; ActiveRecord::Base
    def talk
      "Bla bla bla"
    end
  end
&amp;lt;pre&amp;gt;
&lt;/pre&gt;

which looks on your blog in real:
&lt;pre name="code" class="ruby"&gt;
  class Person &amp;lt; ActiveRecord::Base
    def talk
      "Bla bla bla"
    end
  end
&lt;/pre&gt;

Oh gosh! How sweet!
And the story goes on...&lt;/p&gt;
&lt;p&gt;You can set some options as mentioned at the &lt;a href="http://code.google.com/p/syntaxhighlighter/wiki/Configuration"&gt;SyntaxHighlighterWiki&lt;/a&gt;.
At the end let me give you these advises:
&lt;ul&gt;
  &lt;li&gt;Escape your HTML characters "&lt;" and "&gt;" by "&amp;amp;lt;" and "&amp;amp;gt;" inside the snippets&lt;/li&gt;
  &lt;li&gt;Switch off the line breaks of your blog, otherwise you will see tons of "&amp;lt;/br&amp;gt;"&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;So hurry up to make your own RockinRails blog look pretty!&lt;/p&gt;
&lt;strong class="red"&gt;KaBoom!!!&lt;/strong&gt;
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3939706329553834000-1502483539727707913?l=tnt-on-rails.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tnt-on-rails.blogspot.com/feeds/1502483539727707913/comments/default' title='Kommentare zum Post'/><link rel='replies' type='text/html' href='http://tnt-on-rails.blogspot.com/2009/01/class-test.html#comment-form' title='0 Kommentare'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3939706329553834000/posts/default/1502483539727707913'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3939706329553834000/posts/default/1502483539727707913'/><link rel='alternate' type='text/html' href='http://tnt-on-rails.blogspot.com/2009/01/class-test.html' title='How To Blog Ruby'/><author><name>tobago</name><uri>http://www.blogger.com/profile/10561958786057528168</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='29' height='32' src='http://4.bp.blogspot.com/_kv7KERtXJdc/SXo5bPxTILI/AAAAAAAAABE/mayjxroLpQk/S220/avatar.gif'/></author><thr:total>0</thr:total></entry></feed>
