views:

181

answers:

1

I've put together a RoR application and would now like to publish a RESTful web service for interacting with my application.

I'm not sure where to go exactly, I don't want to simply expose my ActiveRecord models since there is some data on each model that isn't needed or shouldn't be exposed via an API like this. I also don't want to create a SOAP solution.

My application is built using Rails 2.3.5 and I hope to move to Rails 3.0 soon after its released.

I'm basically looking for a way to map my ActiveRecord models to "models" that would be exposed via the web service. Is ActiveResource the correct thing to use? What about ActionWebService?

+2  A: 

You can do that through a controller (or controllers). Your RESTful controller actions can define the API for incoming web service requests, and you can render XML or JSON in the response instead of rendering an HTML view.

I'm sure there are more sophisticated ways of doing this, but this is the simple approach.

Greg
Not that you can also override #as_json in your models to decide what values you will expose, and transforms you want to apply. See http://stackoverflow.com/questions/2572284/override-to-json-in-rails-2-3-5/2574900#2574900 for details of using #as_json vs #to_json.
François Beausoleil
Is it considered "better" to override the to_json and to_xml methods on each model? I think what I'm trying to accomplish is something that's going to be maintainable long term, without mucking up my models.
michaeldelorenzo