I just started looking at REST, and am working on a simple app made up
from Authors and Books with an Author has_many :books relationship.
I’m thinking the following resources make sense:
/books
books/book_id
/books/book_id/author
/authors
/authors/author_id
/authors/author_id/books
I.e. listing books, listing authors, looking up a book or author,
listing books for a certain author, and accessing the author of some
specific book.
Is this reasonable, or am I already doing something wrong? I’m
guessing that /books/book_id/author may be a little over the top.
As for routes, I now have the following:
map.resources :books do |book|
book.resource :author
end
map.resources :authors do |author|
author.resources :books
end
According to rake routes, there’s a fair amount of controller+action
duplication here.
Is this ok, should I be looking at parameters to figure out what to do?
Here’s the index action from my books controller right now. This one
isn’t too messy, but I’m sure other actions will turn out worse…?
GET /books[.xml]
GET /authors/author_id/books[.xml]
def index
if params[:author_id].nil?
@books = Book.find(:all)
else
@books = Author.find(params[:author_id]).books
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @books }
end
end
TBH I’m a little lost, and should probably be looking for books or
something. Recommendations appreciated.
Thank you,
Isak