2012-01-25 76 views
1

我是Ruby on Rails的新手。我遵循these instructions試圖爲我的應用程序創建一個REST API。無法爲我的RoR應用程序創建REST API

map.connect_resource :book(在文檔的第三頁提到的)導致以下錯誤,執行rake test:functionals時:

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

在我的應用程序,我想使用MySQL來實現回報率符合下表的數據。

表名:對象
領域:OBJECT_ID,OBJECT_NAME,Object_description等等

我想創建一個REST API對象用於查詢上述數據庫和檢索數據。最好的方式是什麼?

回答

2

這是一個reallyyyyyyyy老教程(從6年前!!!)。我建議閱讀本指南,而不是:http://guides.rubyonrails.org/routing.html

假設你正在運行的Rails 3,您應該只是把這個在您的routes.rb文件:

resources :books 

,將暴露你的BooksController路由,使您可以訪問:

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

感謝Buddy ... :)這清除了路由問題。請幫我弄清楚我提到的API。 –

+0

我不確定你的意思...... Rails爲你提供API。如果你想要一個JSON api,那麼在你的控制器動作中添加'format.json'。否則,它提供了一個XML API。順便說一句,請點擊我的答案旁邊的複選標記來關閉此問題:) – iwasrobbed

相關問題