【実装方法】
Railsが標準で対応してくれるのでジェネレーターでRest対応コードを生成してくれます。
1 2 3 |
<textarea class="ruby" cols="60" rows="5" name="code"> ./script/generator Scaffold Article </textarea> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
<textarea class="ruby" cols="60" rows="5" name="code"> class ArticlesController < ApplicationController # GET /articles # GET /articles.xml def index @articles = Article.find(:all) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @articles } end end # GET /articles/1 # GET /articles/1.xml def show @article = Article.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @article } end end # GET /articles/new # GET /articles/new.xml def new @article = Article.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @article } end end # GET /articles/1/edit def edit @article = Article.find(params[:id]) end # POST /articles # POST /articles.xml def create @article = Article.new(params[:article]) respond_to do |format| if @article.save flash[:notice] = 'Article was successfully created.' format.html { redirect_to(@article) } format.xml { render :xml => @article, :status => :created, :location => @article } else format.html { render :action => "new" } format.xml { render :xml => @article.errors, :status => :unprocessable_entity } end end end # PUT /articles/1 # PUT /articles/1.xml def update @article = Article.find(params[:id]) respond_to do |format| if @article.update_attributes(params[:article]) flash[:notice] = 'Article was successfully updated.' format.html { redirect_to(@article) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @article.errors, :status => :unprocessable_entity } end end end # DELETE /articles/1 # DELETE /articles/1.xml def destroy @article = Article.find(params[:id]) @article.destroy respond_to do |format| format.html { redirect_to(articles_url) } format.xml { head :ok } end end end </textarea> |
Scaffold時にマッピングも自動的に行ってくれます。
1 2 3 4 5 6 |
<textarea class="ruby" cols="60" rows="5" name="code"> # config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :articles </textarea> |
以上で完了です。
各URLとメソッドの対応表は以下のようになります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<textarea class="ruby" cols="60" rows="5" name="code"> GET /articles.xml {:controller=>"articles", :action=>"index"} POST /articles.xml {:controller=>"articles", :action=>"create"} GET /articles/new.xml {:controller=>"articles", :action=>"new"} GET /articles/:id/edit.xml {:controller=>"articles", :action=>"edit"} GET /articles/:id.xml {:controller=>"articles", :action=>"show"} PUT /articles/:id.xml {:controller=>"articles", :action=>"update"} DELETE /articles/:id.xml {:controller=>"articles", :action=>"destroy"} GET /articles.xml {:controller=>"articles", :action=>"index"} POST /articles.xml {:controller=>"articles", :action=>"create"} GET /articles/new.xml {:controller=>"articles", :action=>"new"} GET /articles/:id/edit.xml {:controller=>"articles", :action=>"edit"} GET /articles/:id.xml {:controller=>"articles", :action=>"show"} PUT /articles/:id.xml {:controller=>"articles", :action=>"update"} DELETE /articles/:id.xml {:controller=>"articles", :action=>"destroy"} </textarea> |