Feed on
Posts
Comments

rails2.0を試してみました。
とりあえずrails hogeとしてみて、script/generate scaffold test name:stringをやってみたのですが、

def show
  @test = Test.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @test }
  end
end

このようなものに遭遇したり、config/routes.rbに

ActionController::Routing::Routes.draw do |map|
  map.resources :tests

こんなのが追加されていたり、/test/show/1にアクセスすると/test/1にURLが変更されて表示されていたり目新しい世界なのでびっくりさせられました。

どうもRESTという考え方が導入されたようです。

$rake routes

を実行すると

              tests GET    /tests                       {:action=>"index", :controller=>"tests"}
    formatted_tests GET    /tests.:format               {:action=>"index", :controller=>"tests"}
                    POST   /tests                       {:action=>"create", :controller=>"tests"}
                    POST   /tests.:format               {:action=>"create", :controller=>"tests"}
           new_test GET    /tests/new                   {:action=>"new", :controller=>"tests"}
 formatted_new_test GET    /tests/new.:format           {:action=>"new", :controller=>"tests"}
          edit_test GET    /tests/:id/edit              {:action=>"edit", :controller=>"tests"}
formatted_edit_test GET    /tests/:id/edit.:format      {:action=>"edit", :controller=>"tests"}
               test GET    /tests/:id                   {:action=>"show", :controller=>"tests"}
     formatted_test GET    /tests/:id.:format           {:action=>"show", :controller=>"tests"}
                    PUT    /tests/:id                   {:action=>"update", :controller=>"tests"}
                    PUT    /tests/:id.:format           {:action=>"update", :controller=>"tests"}
                    DELETE /tests/:id                   {:action=>"destroy", :controller=>"tests"}
                    DELETE /tests/:id.:format           {:action=>"destroy", :controller=>"tests"}
                           /:controller/:action/:id
                           /:controller/:action/:id.:format

と表示されて、scaffoldでは、その使われるindex create new edit show update destroyのアクションは「map.resources :tests」の一文で上記のrouteが設定されているようです。GETやPOSTで挙動が変わるというのは解りやすいと言うか慣れるのに時間がかかりそうです。

scaffoldでこんなことさせられると使う側も意識せざるを得ないので、さっそくRESTとroute.rbを勉強しなくてはいけないハメになりました。

API読むの久々です。


RESTful Webサービス

ご購入しました。


こいつのPDF版もポチリました。

Leave a Reply