이전 튜토리얼 (~ 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
Thanks Buddy : :) 라우팅 문제가 해결되었습니다. 내가 언급 한 API를 이해하도록 도와주세요. –
당신이 무엇을 의미하는지 모르겠습니다 ... 레일스는 API를 제공합니다. JSON API를 원한다면 컨트롤러 액션에'format.json'을 추가하십시오. 그렇지 않으면 XML API를 제공합니다. 그건 그렇고, 내 대답 옆에있는 체크 표시를 치아 하여이 질문을 닫습니다 :) – iwasrobbed