2012-01-25 3 views
1

Ruby on Rails를 처음 사용했습니다. 내 앱의 REST API를 만들기 위해 these instructions을 따라갔습니다.RoR 앱용 REST API를 만들 수 없습니다.

map.connect_resource :book합니다 (문서의 세 번째 페이지에 언급 된) rake test:functionals 다음과 같은 오류, 실행 원인 : 내 응용 프로그램에서

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

, 나는 다음과 같은 테이블 데이터와 MySQL과의 RoR을 구현하기 위해 노력하고있어 .

테이블 이름

: 개체
분야 : OBJECT_ID, OBJECT_NAME, Object_description 등 ...

내가 데이터를 위의 데이터베이스를 조회하고 검색하기위한 REST API의 객체를 생성하고 싶습니다. 계속 진행하는 가장 좋은 방법은 무엇입니까?

답변

2

이전 튜토리얼 (~ 6 년 전 !!!)입니다. 내가 대신이 가이드를 읽고 추천 할 것입니다 : http://guides.rubyonrails.org/routing.html

당신이 레일 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

Thanks Buddy : :) 라우팅 문제가 해결되었습니다. 내가 언급 한 API를 이해하도록 도와주세요. –

+0

당신이 무엇을 의미하는지 모르겠습니다 ... 레일스는 API를 제공합니다. JSON API를 원한다면 컨트롤러 액션에'format.json'을 추가하십시오. 그렇지 않으면 XML API를 제공합니다. 그건 그렇고, 내 대답 옆에있는 체크 표시를 치아 하여이 질문을 닫습니다 :) – iwasrobbed

관련 문제