Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 868
Yagiz Avatar asked Sep 24 '26 20:09

Yagiz


2 Answers

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

like image 138
Rodrigo Zurek Avatar answered Sep 27 '26 20:09

Rodrigo Zurek


You can use this one called rails-api.

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

like image 41
scaryguy Avatar answered Sep 27 '26 19:09

scaryguy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!