无法为我的RoR应用程序创建REST API

时间:2012-01-25 05:10:03

标签: ruby-on-rails api rest

我正在尝试为我的应用创建REST API。

执行map.connect_resource :book时,rake test:functionals会导致以下错误:

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

在我的应用程序中,我正在尝试使用以下表数据实现带有MySQL的RoR。

Table Name: Object  
Fields: object_id, Object_name, Object_description etc...

我想创建一个REST API对象,用于查询上述数据库并检索数据。什么是最好的方法?

1 个答案:

答案 0 :(得分:2)

这是一个非常糟糕的旧教程(从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