Hey there,
The best thing to do is to setup a before_filter in your application controller that checks for the iPhone Mime-Type. Go to config/initializers/mime_types.rb and add this line
Mime::Type.register_alias "text/html", :iphone
And here is the method that gets called in a before filter in my ApplicationController.
def mobile_user_agent?
agent = request.env['HTTP_USER_AGENT']
return false if !agent
return :iphone if agent[/(Mobile\/.+Safari)/]
return :iemobile if agent[/IEMobile/]
return :blackberry_4_3 if agent[/BlackBerry.*4.3.0/]
return :blackberry_4_7 if agent[/BlackBerry.*4.7.0/]
return :android if agent[/Android/]
return false
end
Then in your controllers you can do the following in your controllers.
def index
@posts = Post.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
format.iphone # render index.html.iphone
end
end
This lets you render iPhone specific views. You can do the same thing for all kinds of Mobile phones, Android, Blackberry, Windows Mobile etc...
I can't take all of the credit for this :) Mike Clark and the Pragmatic Programmers featured a solution like this in Advanced Rails Recipes.
My only other advice would be that if you are doing a lot of different iPhone stuff in your app, then make a separate controller called IPhoneViewController or something that handles all of the iPhone stuff.
Good luck! If you need anything else let me know,
Kent