我有一个本机 iOS 应用程序,可以通过myiosapp://
. 我还有一个简单的 Rails 应用程序,当请求来自移动设备时,它应该重定向到本机应用程序。这就是我遇到问题的地方 - 我不能redirect_to 'myiosapp://
我想尽可能简短地描述这个问题,所以我制作了一个示例应用程序,它去除了不相关的信息,但复制了同样的问题。
这是我的路线.rb:
MyRailsApp::Application.routes.draw do
root :to => 'redirect#index'
end
这是redirect_controller.rb:
class RedirectController < 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
每当我跑去rails server
,localhost::3000
我都会得到这个:
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'
在发布此之前,我已经看到了许多类似的问题,但似乎没有一个更具体地说明我如何在 Rails 中实现它: