Stefan Tilkov's Random Stuff

RESTful Rails

Peter Williams asks for a more RESTful Rails — I don’t know enough about Rails’ internals to judge whether what he asks for is easily doable, but it would surely be nice to have support for the programming model he suggests:

class BooksController < RestController
  uri_base "/books/"

  resource "." do
    get "application/atom+xml" do
      # return XML representation of collection containing all known books.
    end

    get "text/html" do
      # return HTML representation of collection containing all known books.
    end

    post do
      # create new book and add it to the collection
    end
  end

  resource "./:id", :requirements => {:id => /^[[:digit:]]+$/}  do
    get  do
      # yield html representation of a individual book
    end

    put do
      # update and individual book resource
    end
  end

  resource "./:id/editor", :requirements => {:id => /^[[:digit:]]+$/}  do
    get do
      # yield a representation of the book editor (a page with a 
      #   form that puts to "./:id" on submit)
    end   
  end

  resource "./creator" do
    get do
      # yield a representation of the book creator (a page with a 
      #   form that posts to "." on submit)
    end
  end
end

Comments