title: Rails: redirect_to 'myapp://' to call iOS app from mobile safari
body: <p>I have a native iOS app that can be called from Mobile Safari in iOS via <code>myiosapp://</code>. I also have a simple Rails app that SHOULD redirect to the native app when the request is coming from mobile. This is where I am I having problems - I can't <code>redirect_to 'myiosapp://</code></p>
  
  <p>I want to describe this problem as short as possible, so I made a sample app that shaves away the irrelevant information, but replicates the same problem.</p>
  
  <p>Here's my routes.rb:</p>
  
  <pre><code>MyRailsApp::Application.routes.draw do
    root :to =&gt; 'redirect#index'
  end
  </code></pre>
  
  <p>And here's redirect_controller.rb:</p>
  
  <pre><code>class RedirectController &lt; ApplicationController
    def index
      if request_from_mobile?
        redirect_to "myiosapp://"
      else
        redirect_to "/default.html"
      end
    end
  
    private
  
    def request_from_mobile?
      request.user_agent =~ /Mobile|webOS/
    end
  end
  </code></pre>
  
  <p>Whenever I run <code>rails server</code> and go to <code>localhost::3000</code>, I get this:</p>
  
  <pre><code>Started GET "/" for 127.0.0.1 at 2012-09-21 14:00:52 +0800
  Processing by RedirectController#index as HTML
  Redirected to motionapp://
  Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
  [2012-09-21 14:00:52] ERROR URI::InvalidURIError: bad URI(absolute but no path): motionapp://
      /Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/generic.rb:1202:in `rescue in merge'
      /Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/generic.rb:1199:in `merge'
      /Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpresponse.rb:220:in `setup_header'
      /Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpresponse.rb:150:in `send_response'
      /Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:110:in `run'
      /Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
  </code></pre>
  
  <p>Before posting this, I've already seen a number of similar problems, but none seemed to be more specific to how I can implement this in Rails:</p>
  
  <p><a href="https://stackoverflow.com/questions/9688319/how-to-redirect-from-mobile-safari-to-native-ios-app-like-quora">How to redirect from Mobile Safari to Native iOS app (like Quora)?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5182666/iphone-web-app-to-automatically-redirect-to-app">iphone web app to automatically redirect to app</a></p>