What's New in Edge Rails: RESTful Method Support in Link Helpers

Posted by ryan
at 6:27 AM on Tuesday, May 30, 2006

A group of changes have recently been committed to edge Rails that allows the use of the various REST style HTTP methods when creating links. Deprecated are the days of using :post => true to indicate a data-altering request (post being only one of the five main REST methods):

Bye-bye this:

link_to("Delete article", { :action => "destroy", :id => @article },
  :confirm => "Are you sure you want to delete this article?",
  :post => true

And hello to proper REST (note the * :method => :delete* and * :method => :put*)!

link_to("Delete article", { :action => "destroy", :id => @article },
  :confirm => "Are you sure you want to delete this article?",
  :method => :delete)
link_to("Edit article", { :action => "edit", :id => @article },
  :method => :put)

All your favorite helper methods now support REST verbs including link_to, link_to_remote, form_tag, form_for, remote_form_tag and remote_form_for.

Now you can start building links with verbs that actually represent the type of request being made. On the controller side you can determine the request method the same way you always have – with the request.post?, request.get?, request.put?, request.delete?, request.head? methods.

On a related note, here are some links to work being done on providing REST-ful controllers for rails:

tags: REST, rubyonrails, rails

Comments

Leave a response

  1. somekoolJuly 04, 2006 @ 07:43 AM
    link_to could also be alias to delete_to post_to get_to put_to or equivalent ?
  2. somekoolJuly 04, 2006 @ 07:43 AM
    why do we still have to specify :action => 'destroy' ? and as people are not used to PUT/POST terms (which one is create, update?), maybe aliases could be handy. so it would become link_to("Delete article", { :id => @a }, :confirm => 'I warned you', :method => :destroy) link_to("Edit article", { :id => @a }, :method => :edit) # :update? thanks
  3. rickJuly 28, 2006 @ 05:16 PM
    and my textile is all wrong. <pre><code> # delete link link_to ‘delete’, :url => article_url(@article), :method => :delete # edit link link_to ‘edit’, edit_article_url(@article) # update form form_for :article, :url => article_url(@article), :method => :put </code></pre>
  4. rickJuly 28, 2006 @ 05:16 PM
    and my textile is all wrong. <notextile> # delete link link_to ‘delete’, :url => article_url(@article), :method => :delete # edit link link_to ‘edit’, edit_article_url(@article) # update form form_for :article, :url => article_url(@article), :method => :put </notextile> or http://pastie.caboo.se/7029
  5. JamieJuly 28, 2006 @ 05:16 PM
    I've just encountered problem while using :method => :delete on a link_to_remote, works fine in Firefox but not Safari. Any ideas?
  6. rickJuly 28, 2006 @ 05:17 PM
    Your links are all wrong. # delete link link_to 'delete', :url => article_url(@article), :method => :delete # edit link link_to 'edit', edit_article_url(@article) # update form form_for :article, :url => article_url(@article), :method => :put
  7. Piyush GUptaOctober 16, 2006 @ 03:26 AM
    hi there mail me some expalnationa about the link to remote helper on 2 [email protected]