Exposing web service API in ROR 3
I would really appreciate advice on how to best expose certain functionality from my site via a service API .. from my searches online I found that ActionWebService used to offer certain functionality in that direction but has been deprecated in the latest version of rails.
To elaborate a bit, I 开发者_如何学运维would like to build an iPhone app connecting to my current rails website.. ideally an "API services" model which contains a bunch of functions that would be exposed on HTTP:///Services for example where everything my iPhone would ever need be provided ... so essentially those Services functions grab data from other models and just offer it in an exposed API.
Looking for advice if I have a correct approach here and what is the syntax to expose API in rails?
Thank you!
Rails makes this really easy, actually. It exposes XML endpoints by default. You can expose JSON just as easy (and, for iPhone, you should use JSON since it's easier to parse). Plus, it does this RESTfully. You shouldn't add an API controller — just use the controllers you already have for your resources.
In your respond_to
block, you should already see the code for XML. For JSON, just add the following:
format.json { render :json => @item, :status => :created, :location => @item }
Hook this up with something like RestKit on the iPhone and you can easily create an iPhone app that communicates with a Rails app.
精彩评论