The Rails resource provides a RESTful interface for your model. We'll see.
Model
class Contact < ActiveRecord::Base ... end
Routes
map.resources :contacts
Controller
class ContactsController < ApplicationController ... def show @contact = Contact.find(params[:id] respond_to do |format| format.html format.xml {render :xml => @contact} format.js {render :json => @contact.json} end end ... end
This way, it gives you APIs without having to define special methods to get the type of response you need.
Eg.
/contacts/1 # Responds with regular html page /contacts/1.xml # Responds with xml output of Contact.find(1) and its attributes /contacts/1.js # Responds with json output of Contact.find(1) and its attributes
JasonOng
source share