banditKing
banditKing

Reputation: 9579

Coupling Ruby On Rails with native iPhone client

Hi Im new to Ruby On Rails, but have experience building iPhone apps. So far my iPhone apps have been built natively and they all have had a PHP back end. I simply used to invoke a PHP script from a Http request from the iPhone to communicate with the server.

Now I want to use RoR for the back end. However, RoR as a MVC architecture. The part thats confusing is, Since my "View" is not really a Html page, since its a native iPhone app, can I still use RoR as the back end. If so, then in place of the Html files in RoR's folders what should I put, since there is no view to render. The only thing that I want the controller to do after it receives a HttP request from a client is respond with a JSON object to the iPhone.

ANy ideas/suggestions. Could someone please give me some links or names of books that deal with this scenario. I tried searching online, but couldn't find this situation, maybe I was searching using the wrong keywords.

Upvotes: 1

Views: 270

Answers (2)

JoshSchlesinger
JoshSchlesinger

Reputation: 957

You won't need the views at all for your iphone app. Your index action would have something like this...

def index  
    @posts = Post.all
    respond_to do |format|
        format.html  # index.html.erb    
        format.json  { render :json => @posts }  
    end
end

in respons_do you see format.html would return your html view (if you don't have a veiw just remove this line). The second line is what you are interested in. format.json will return a json response instead of rendering an html view. Calling http://example.com/posts.json would return your @posts as json.

You can see the full example here http://guides.rubyonrails.org/getting_started.html

Upvotes: 1

Alan
Alan

Reputation: 46823

What you'd likely do is build a REST based service using Ruby on Rails.

As you wrote, the views aren't really necessary, unless you want to have a pretty page for any web browsers that consume your content.

Upvotes: 0

Related Questions