我参与了 HTML5 应用程序的构建,我们已经为 iPhone、Android 等创建了自定义 MIME 类型(移动)。例如,我们有 index.html.haml 和 index.mobile.haml。在许多情况下,输出将是相同的——所以我的问题是,如何创建一个同时被移动和 html 请求格式使用的 View 文件?在 View 不同的情况下,选择正确的 html 或移动 View ?
我一直在挖掘,似乎找不到一个干净优雅的解决方案。
最佳答案
您可以在同一个 respond_to
block 中处理常规请求和移动请求,并明确设置要使用的模板。例如:
respond_to do |format|
format.any(:html, :mobile)
render :template => "action.html.erb"
end
end
但是,我最近读了一篇文章,其中避开了移动设备特定的请求格式,以实现一种“级联” View 。您可能还想考虑一下:Mobile Devices and Rails: Maintaining your Sanity .
关于ruby-on-rails - 多种请求格式的单一 View 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4919427/