Yagiz
Yagiz

Reputation: 1033

Rails Mobile API

I have an existing Rails project which uses Omniauth-facebook gem to connect to gem. Only facebook users can interact and use the website. I am now developing a mobile application, and want to build an API for the current Rails project.

Which gem should I use to build an API? Should I use two different Rails projects, one for website and one for API?

Thank you.

Upvotes: 3

Views: 1722

Answers (2)

scaryguy
scaryguy

Reputation: 7940

You can use this one called rails-api.

A cool screencast is here: http://railscasts.com/episodes/348-the-rails-api-gem

Upvotes: 3

Rodrigo Zurek
Rodrigo Zurek

Reputation: 4575

Rails is already API ready, here is a quick example

lets imagine you want to show all the products of your app in a mobile app

lets go to the products controller:

Controller:

  def index
    @products = Product.all       

    respond_to do |format|
      format.js
      format.html
      format.json { render json: @products }
    end
  end

as you can see you can render the json within the controller, then in your mobile app you can do a request on that with the json extension something like this:

$.post('www.example.com/products.json', function(data) {
  alert(data);
});

now you adapt your mobile app to read that json, and thats basicly how it works

Upvotes: 4

Related Questions