Respond_to and format


#1

Hi,

I’m creating a new format for an iPhone/iPod connections.

My problem is that iPhones/iPods always receive the .html pages.

If I comment the format.html # index.html.erb then the devices receive
the .xml format, and If I coment this one they receive the format.iphone
wich is the expected.

In the .html pages I add some code to test if the device is really an
iPhone or not:

<% if iphone_user_agent? # Show message for iPhone users -%>

Using an iPhone? Use the optimised version.

<%else%>

Welcome iPhone user!

<% end -%>

And when the device is an iPhone/iPod I get the Using an iPhone? message

Of course I’m connecting from an iPhone/iPod or through the iPhone
simulator from Xcode.

When I use a simple browser from a computer I expect the .html versions,
wich I’m receiving as default no matter wich device/browser is
connected.

What did I miss ?

thanks,

r.

In config/initializers/mime_types.rb

Be sure to restart your server when you modify this file.

Add new mime types for use in respond_to blocks:

Mime::Type.register “text/richtext”, :rtf

Mime::Type.register_alias “text/html”, :iphone

class ScannsController < ApplicationController

around_filter :login_required

def index

@scanns = Scann.find_by_sql("SELECT * FROM totals_diaris_avui")
@page_title="Scanns of today"

  respond_to do |format|
    format.iphone  # action.iphone.erb
  format.html # index.html.erb
  format.xml  { render :xml => @scanns }
  end

end

end


#2

Raimon Fs wrote:

  respond_to do |format|
    format.iphone  # action.iphone.erb
  format.html # index.html.erb
  format.xml  { render :xml => @scanns }

It may require someone else to answer this properly however I think you
are getting confused about what will actually happen:

The iphone will always receive the html pages because the
webserver/rails app by default will look for html pages.

for example:

  1. http://www.mysite.com/ #=> deafult format.html
  2. http://www.mysite.com/index.xml #=> format.xml
  3. http://www.mysite.com/index.iphone #=> format.iphone

Perhaps you could change default route to point to index.iphone when you
use your subdomain. You’d then need to ensure all your links used the
.iphone extension.

Unless there is someway to force the default format from html to
‘iphone’? Wouldn’t surprise me if it was possible, however, you’d need
to look it up…

Cheers
Luke


#3

Luke P. wrote:

Raimon Fs wrote:

  respond_to do |format|
    format.iphone  # action.iphone.erb
  format.html # index.html.erb
  format.xml  { render :xml => @scanns }

It may require someone else to answer this properly however I think you
are getting confused about what will actually happen:

The iphone will always receive the html pages because the
webserver/rails app by default will look for html pages.

for example:

  1. http://www.mysite.com/ #=> deafult format.html
  2. http://www.mysite.com/index.xml #=> format.xml
  3. http://www.mysite.com/index.iphone #=> format.iphone

Perhaps you could change default route to point to index.iphone when you
use your subdomain. You’d then need to ensure all your links used the
.iphone extension.

Unless there is someway to force the default format from html to
‘iphone’? Wouldn’t surprise me if it was possible, however, you’d need
to look it up…

Cheers
Luke

Hello,

Do you mean to always use for example an iphone as an extension ?

Then I would to add some special route, don’t ?

And in the controller, split the actions:

index

index_iphone

but this approach forces to have duplicate methods, for example fetching
the same data in two different places, I can’t follow the DRY. I know at
least I must have differents views …

If the respond_to and format would work, it would be great …

anyone know if this is possible ?

thanks!

r.


#4

Hello,

ok, I solved it with the hard-way …

:slight_smile:

I was not setting the format to iphone correctly in the application
controller, the iPhone format only was available if the user connects
from a subdomain iphone.mywebpage.com or via a custom parameter,
format=iPhone.

As I’m developing locally with the 127.0.0.1 I don’t have a quick-way to
simulate iphone.127.0.0.1 or something similar, so I change the method
and simply if the user connected is from an iPhone/iPod I show their
pages directly.

I’ll think in a better solution, but as this is for a test purposes, at
this moment it’s ok.

thanks,

r.