androidiosruby-on-rails

JSON or HTML: Designing my server side to reduce latency and support multiple clients


I'm a new rails and web developer. I have some experience with iOS.

I'm creating a web application for the first time. However, these days all web application must be supported by iPhone, Android and other clients. After spending 2-3 months understanding rails and then HTML, JavaScript, jQuery and writing code. I'm getting the realization that there is latency in rendering HTML and web pages.

So I was thinking if I want to make a web application, I want to make it modular by design. What I mean is I want to only render JSON in every method and let the clients figure out how to render and design the views. Keeping this in mind, I searched the web for solutions and saw people talking about creating an API.

I read up a bit about it, but my knowledge still has holes. So I would like to know how can I design a server side of a web app that can be used by different clients: iOS, Android and browsers. Is there a certain design philosophy? Dos? Don'ts? Any caveats? What are the technologies involved in this?

There seems to be a lot of scattered info on the web, but is there 1 place where I can learn about everything or most things I need to know to get this app running?

I would really appreciate your help.


Solution

  • Rails makes it very simple to display information in various formats. The respond_to method specifically in Rails 3.1, is capable of letting a controller send data to the client in different ways. Here's an example:

    def index
      @categories = Category.query(params)
    
      respond_to do |format|
        format.html
        format.json { render_for_api :complete, :json => @categories }
      end
    end
    

    If the browser/iPhone/Android device makes a request to the index action, the controller will respond with the html and the according view, index.html.erb, but if the device requests a index.json, rails controller will respond with a representation of the object on JSON notation. (Here i am using a gem called acts_as_api to facilitate my json creating thing).

    In my opinion there's no certain way to do it, but Rails supports development by making it easy to extend your app onto different devices.