看看Rails Mobile
我不久前开发了那个插件。该插件背后的想法是您可以通过路由器配置文件根据您的移动设备功能重定向到不同的控制器或视图。
在 routing.rb 的末尾添加以下几行:
MobileDispatch::Categories.add do
def mobile_classifier(device)
"_mobile"
end
end
这些行为所有移动设备定义了一个新的子字符串,该子字符串将存储在 roging.rb 文件中每个请求的 $ 变量中。
这样您就可以使用您的路由规则。例如 routing.rb 中的这一行:
匹配 '/photo/:id', :to => "photo#index$", :classifier => :mobile_classifier
对于普通用户会被解释为:
匹配 '/photo/:id', :to => "photo#index", :classifier => :mobile_classifier
对于移动用户来说:
匹配 '/photo/:id', :to => "photo#index_mobile", :classifier => :mobile_classifier
这里的强大之处在于 mobile_classifier(device) 方法,您可以根据设备对象返回不同的分类。
假设我们修改方法,为所有 iphone 设备返回“_iphone”,为所有 android 手机返回“_android”,那么上面的路由行将被解释为:
匹配 '/photo/:id', :to => "photo#index_iphone", :classifier => :mobile_classifier
匹配 '/photo/:id', :to => "photo#index_android", :classifier => :mobile_classifier
如果您将 $ 添加到每个路由的视图末尾部分(类似于我们在此处所做的),您将在控制器中为每种设备类别获得不同的方法,并为每种方法获得不同的视图名称(index_iphone.htm.htm)。 erb 和 index_android.ht.erb) 这样您就可以为您在 mobile_classifier 方法中定义的每个设备类别提供单独的视图/层。