1

EDITED Question

I understand that I can't just throw Rails code into Phonegap and I have to write create some static pages and use JS to do the server communication for data. My question is how to write Rails code to handle http request to return a JSON with information?

Thanks

OLD Question

I built a Rails mobile app and I am interested in using Phonegap to turn it into a native iPhone and Android app.

I searched around and wasn't really able to find a good tutorial on how to do this. I watched the Quick Left video and I am confused why the extra middle wear code was needed. From my understanding, to use Phonegap I need to rewrite all the front end and data fetching in javascript.

If anyone could offer some insight or point me to some tutorial to how I could integrate my Rails app with Phonegap please let me know.

Thanks

Pan Wangperawong
  • 1,250
  • 2
  • 13
  • 29

3 Answers3

2

I do not think this is possible. Ruby would need an interpreter and unless it is in the browser, it cant run. I think the only way is to use javascript

jordan koskei
  • 2,749
  • 1
  • 15
  • 12
0

You can´t, but maybe you want to try Rhodes: http://rhomobile.com/products/rhodes/

0

Rails 3.1.x already returns JSON requests if you have a controller like this:

class EaracheMyEyeController < ApplicationController

  def show
    @earache_my_eye = EaracheMyEye.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @earache_my_eye }
    end
  end
end

The format.json { render json: @earache_my_eye } part will render the data as JSON if you visit the URL of the object, for example earachemyeyes/1.json

So, in your phone gap app you would call URLS GET method to receive data and add on .json on the end of the url to receive JSON formatted data.

Update:

A few things I learned recently about JSONP requests in phonegap. They are only GET requests. No posts.

However, you can append a _method=POST, and add configure.middleware.swap(Rack::MethodOverride,Rack::RestfulJsonpMiddleware to your conf/environments/(production/development/test).rb file(s)

Add something like this to your library:

https://github.com/quickleft/kbomb_phonegap_server/blob/master/lib/rack/restful_jsonp_middleware.rb

This allows you to send an actual GET request, yet it is read and processed as your _method=POST or whatever method you really need. You can't use the built in Rack::MethodOverride because it only implements POST, and nothing else (was meant to facilitate PUT and GET). So the file at https://github.com/quickleft/kbomb_phonegap_server/blob/master/lib/rack/restful_jsonp_middleware.rb builds a new Rack middleware that lets you use all HTTP methods.

Sam Rose
  • 66
  • 1
  • 5