开发者_StackOverflow中文版
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this questionI am looking to build a REST webservice on Rails to serve as the server side component to my mobile app. Can someone point out some tutorials or screencasts on building REST webservices for someone who is learning Rails?
The main features I am looking for are:
- Authorization(user id and password validation).
- Posting data from the client (mobile app) to the server database using REST API
I am also okay if you could point me to some book where content on creating a rest api is available.
It would also be great if someone could post a bit of code to get me started on this.
I am relatively new to Rails and have almost finished the book "Ruby on Rails Tutorials" by Michael Hartl. This is my only experience with Rails, so far.
Thanks.
I wrote 2 blog posts that you'll probably find helpful:
http://davidsulc.com/blog/2011/04/10/implementing-a-public-api-in-rails-3/
http://davidsulc.com/blog/2011/04/17/consuming-a-public-rails-api-with-jquery/
They're not tutorials, but it should get you going.
The basic idea is: expose JSON data through controller actions when a request is made with the .json
format.
Cross domain requests (like the ones you'll be doing from your mobile app) are a little trickier, as explained in the blog post: you'll actually need to send javascript within a callback, or your code won't work (you'll get an empty response). (Explained in the first post.)
Once the data is exposed as JSON, simply query it from your mobile app (as explained in the 2nd post).
EDIT: Manning has a book on Rails 3 (one of the authors being none other than Yehuda Katz) with a chapter on creating an API: http://manning.com/katz/ In particular, it covers token authorization for the API using Devise.
I'd suggest you visit Railscasts. If you search on various keywords you'll get a wealth of information. I used 'REST', 'routes', 'controller' to get myself started...
精彩评论