I’d been wanting to make my Ruby on Rails based San Francisco Sailing Weather site available for mobile / cell phones. Now that I’m on vacation in Germany I had a stretch on a train ride where I was able to crank out a simple working version. Here are the steps I took:
1. Set the WAP WML content type. You can do this on each controller method, or by using a controller specific before_filter, or an application wide before_filter as I did that applies to all actions with the name wap in app/controllers/application.rb:
before_filter :set_wap_content_type, <span>:</span>only => :wap
</small>
def set_wap_content_type
  @headers["Content-Type"] = "text/vnd.wap.wml; charset=iso-8859-1"
end
</small>
2. Next I created an app/views/layouts/wap.rhtml decorator to wrap all of my WML documents with:
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card title="<%= @title %>">
<p align="center">
<%= @content_for_layout %>
</p>
</card>
</wml>
</small>
3. Then I added a wap method to my index controller so that users can access the mobile version of the site via http://windandtides.com/wap
def wap
  @title = "San Francisco Sailing Weather"
  render(:layout => "wml")
end
</small>
4. Finally I created a app/views/index/wap.rhtml to provide the content for the home page:
<%= link_to 'NOAA Marine Forecast', { :controller => '/marine/forecast', :action => 'wap' } %><br/>
<%= link_to 'Wind Readings', { :controller => '/marine/wind', :action => 'wap' } %><br/>
<%= link_to 'Current Predictions', { :controller => '/marine/tide', :action => 'wap' } %>
</small>
Here is a demo of the result. You could probably also use your regular actions and adjust the content-type and view you serve based on the accepts header which you can access via the respond_to method. WAP/WML enabling the sub pages turned out to be a snap as I’d already broken out the content into partials from when I added RSS support so I was able to re-use those partials without any additional changes.
Some helpful web tools and references that I used were:
W3schools WAP tutorial
W3schools WML reference
Web based WAP Emulator
Thanks for the helpful write-up Todd. I’m thinking of WAP-enabling my wifi cafes site (http://wifi.earthcode.com), so this is useful.
I would also encourage you to look up the deck WML tag. I’ve found that it is cumbersome to browse WML pages when scrolling is involved. Decks and cards work really well to take that pain out of the process, however you’d have to do a bit more work and alter those partials a bit.
Pingback: Labnotes » Rounded Corners
May I suggest you look into the WURFL SDN Java project and this link http://wurfl.sourceforge.net/ruby/index.php
-E
Hi,
I am a surfer and have been doing some Rails stuff. I just started a rails site that has buoy reports for various places. I have an admin backend so I can add any regions that people would like. I just started out with some east/west coast areas for now. Thanks for the info, I figured out how to set the header info from your site, and was partly inspired from looking at what you did as well, so I knew it was doable. Sorry that my front page looks kind of primitive, html,css and content isn’t my thing, but I may try to get a picture up for the web part of it: http://www.surf.freespiritboston.net
Pingback: 使用Railså¼€å‘支æŒWAP/WML的应用 - IceskYsl@1sters