# Respond\_to and format

**URL:** <https://www.ruby-forum.com/t/respond-to-and-format/158338>\
**Category:** Rails\
**Created:** [January 22, 2009, 8:39am UTC](https://www.ruby-forum.com/t/respond-to-and-format/158338 "2009-01-22T08:39:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [January 22, 2009, 8:39am UTC](https://www.ruby-forum.com/t/respond-to-and-format/158338/1 "2009-01-22T08:39:34Z")

</div>

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](http://iphone.foo.com/).

\<%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

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [January 22, 2009, 5:14pm UTC](https://www.ruby-forum.com/t/respond-to-and-format/158338/2 "2009-01-22T17:14:03Z")

</div>

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/](http://www.mysite.com/) #=\> deafult format.html
2. [http://www.mysite.com/index.xml](http://www.mysite.com/index.xml) #=\> format.xml
3. [http://www.mysite.com/index.iphone](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

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [January 23, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/respond-to-and-format/158338/3 "2009-01-23T19:42:44Z")

</div>

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/](http://www.mysite.com/) #=\> deafult format.html
> 2. [http://www.mysite.com/index.xml](http://www.mysite.com/index.xml) #=\> format.xml
> 3. [http://www.mysite.com/index.iphone](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.

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [January 23, 2009, 8:33pm UTC](https://www.ruby-forum.com/t/respond-to-and-format/158338/4 "2009-01-23T20:33:16Z")

</div>

Hello,

ok, I solved it with the hard-way …

🙂

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](http://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.
