General question about rails controllers

4 views
Skip to first unread message

arwed

unread,
Feb 24, 2011, 6:04:02 AM2/24/11
to ObjectiveResource
I have a finished rails app for the web.
Now I want to interact with it with an iPhone app.

Do I create separate controllers for the communication with the
iphone, or how do I separate the logic for the iphone from the logic
for the web.

Wes Duff

unread,
Feb 24, 2011, 10:40:47 AM2/24/11
to objectiv...@googlegroups.com
With the web you are referencing the HTML layouts. Now you are accessing the XML. It's all about REST.
You don't necessarily have to create new controllers but you can if you wish, or you can make new blocks of code inside your current controllers.
That way you can keep your code light. But if your app gets to crazy it might be a good idea to make new controllers.
Wes Duff

> --
> You received this message because you are subscribed to the Google Groups "ObjectiveResource" group.
> To post to this group, send email to objectiv...@googlegroups.com.
> To unsubscribe from this group, send email to objectiveresou...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/objectiveresource?hl=en.
>

Fredrik Jansson

unread,
Feb 24, 2011, 10:44:59 AM2/24/11
to objectiv...@googlegroups.com
No the iphone communicates through http put get post requests. So you just need to render xml or json in your controllers 

Aubrey Goodman

unread,
Feb 24, 2011, 2:13:10 PM2/24/11
to objectiv...@googlegroups.com
I recommend namespacing your web service (XML/json) controllers and extending your existing controllers to inherit behavior, like this:

class SongsController < ActionController
before_filter :assign_songs, :only => :index

def index
end

private
def assign_songs
@songs = Song.all
end
end

class Api::SongsController < SongsController
def index
respond_to do |format|
format.json { render :json => @songs }
format.xml { render :xml => @songs }
end
end
end

This makes it easier to separate your tests. It also allows you to extend the controller, if you need extra or different logic in your web service than you have in your existing controller.

Hope this helps.
-Aubrey

Reply all
Reply to author
Forward
0 new messages