2012-01-25 14 views
1

Ruby on Railsの新機能です。私はthese instructionsに続いて、私のアプリ用のREST APIを作成しようとしました。 rake test:functionalsを実行するときにRoRアプリケーション用のREST APIを作成できません

(DOCの3ページ目に記載された)map.connect_resource :bookは、次のエラーが発生します。

Error: undefined local variable or method `map' for # 
<ActionDispatch::Routing::Mapper:0x8a11e74>. 

を私のアプリでは、私は、次の表のデータをMySQLでRoRのを実装しようとしています。

テーブル名:オブジェクト
フィールド:OBJECT_ID、OBJECT_NAME、Object_description等...

私は上記のデータベースを照会し、データを取得するためのREST APIオブジェクトを作成したいと思います。進むべき最善の方法は何ですか?

答えて

2

これは実際にはちょっと古いチュートリアルです(から6年前!!!)。私が代わりにこのガイドを読んで推薦:

resources :books 

ので、あなたがアクセスすることができ、あなたのBooksControllerのためのルートを公開します:

http://guides.rubyonrails.org/routing.html

あなたがRailsの3を実行していると仮定すると、あなたは自分のroutes.rbファイルでこれを置く必要がありますそうで

HTTP Verb Path    action  used for 
----------------------------------------------------------------------- 
GET   /books   index  display a list of all books 
GET   /books/new  new   return an HTML form for creating a new book 
POST  /books   create  create a new book 
GET   /books/:id  show  display a specific book 
GET   /books/:id/edit edit  return an HTML form for editing a book 
PUT   /books/:id  update  update a specific book 
DELETE  /books/:id  destroy  delete a specific book 

BooksControllerあなたがして持っているでしょう:

class BooksController < ApplicationController 
    # GET /books 
    # GET /books.xml 
    def index 
    @books = Book.all 

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

    # GET /books/1 
    # GET /books/1.xml 
    def show 
    @book = Book.find(params[:id]) 

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

    # GET /books/new 
    # GET /books/new.xml 
    def new 
    @book = Book.new 

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

    # GET /books/1/edit 
    def edit 
    @book = Book.find(params[:id]) 
    end 

    # POST /books 
    # POST /books.xml 
    def create 
    @book = Book.new(params[:book]) 

    respond_to do |format| 
     if @book.save 
     format.html { redirect_to(@book, :notice => 'Book was successfully created.') } 
     format.xml { render :xml => @book, :status => :created, :location => @book } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @book.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /books/1 
    # PUT /books/1.xml 
    def update 
    @book = Book.find(params[:id]) 

    respond_to do |format| 
     if @book.update_attributes(params[:book]) 
     format.html { redirect_to(@book, :notice => 'Book was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @book.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /books/1 
    # DELETE /books/1.xml 
    def destroy 
    @book = Book.find(params[:id]) 
    @book.destroy 

    respond_to do |format| 
     format.html { redirect_to(books_url) } 
     format.xml { head :ok } 
    end 
    end 
end 
+0

ありがとうございました... :) thatsはルーティングの問題を解決しました。私が言及したAPIの事を理解するのを助けてください! –

+0

あなたは何を意味するのか分かりません... RailsはAPIを提供しています。 JSON APIが必要な場合は、コントローラアクションに 'format.json'を追加してください。それ以外の場合は、XML APIを提供します。ところで、私の答えの隣にあるチェックマークを押すことでこの質問を閉じてください:) – iwasrobbed

関連する問題