Forum: Ruby on Rails respond_to and format

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D48be99143e13e40d1c44a4da63a53f4?d=identicon&s=25 Mongeta 99 (raimon)
on 2009-01-22 08:39
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 -%>
<div class="message">
    <p>Using an iPhone? <a href="http://iphone.foo.com/">Use the
optimised version</a>.</p>
</div>
<%else%>
<p>Welcome iPhone user!</p>
<% 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
85f8b7bbe07bff22e0b38cca818cf692?d=identicon&s=25 Luke Pearce (kule)
on 2009-01-22 17:14
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
D48be99143e13e40d1c44a4da63a53f4?d=identicon&s=25 Mongeta 99 (raimon)
on 2009-01-23 19:42
Luke Pearce 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.
D48be99143e13e40d1c44a4da63a53f4?d=identicon&s=25 Mongeta 99 (raimon)
on 2009-01-23 20:33
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 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.
This topic is locked and can not be replied to.