【问题标题】:render different files for mobile vs desktop为移动和桌面呈现不同的文件
【发布时间】:2011-05-23 23:57:00
【问题描述】:

我正在使用http://padrinorb.org (haml+lesscss) 为移动(android/iPhone/iPad)和桌面用户编写一个小型网络应用程序,因此,我希望显示稍微不同的 html/css不同的设备。

为了确定我使用 rack-mobile-detect 的各种客户端,到目前为止效果很好。

为了让布局成为 layouts/application.mobile,我将这些帮助器/包装器用于render

def preferred_extension 
  if request.xhr?
    "js"
  elsif env["X_MOBILE_DEVICE"]
    "mobile"
  else
    "html"
  end 
end 

def preferred_layout 
  if preferred_extension.eql? "html"
    "application"
  else
    "application.#{preferred_extension}"
  end 
end 

def render_preferred filename
  filename = "#{filename}.#{preferred_extension}"
  if request.xhr?
    layout_file = false
  else
    layout_file = "layouts/#{preferred_layout}".to_sym
  end
  render filename, :layout => layout_file
end 

但是,在处理部分时,这对我没有帮助......而且感觉必须有人对此有更好的解决方案,而不仅仅是将渲染和部分包装在帮助器中。

我想要的是不要在我的控制器和视图中撒上奇怪的代码,只是为了用正确的布局呈现正确的部分/文件。

我希望渲染发现找出要使用的文件,如果没有文件名,则回退到 filename.haml。#{preferred_extension}.haml。

在 Rails 中,我在以前的项目中使用过这种 mime 类型的东西,但我没有找到任何与 padrino (sinatra) 类似的东西

【问题讨论】:

    标签: ruby mobile sinatra haml padrino


    【解决方案1】:

    我通过将 @_content_type 设置为每个 app/controllers/ 内的 before 块中的 preferred_extension 解决了这个问题

    【讨论】:

    • 现在应该可以使用 params[:format] = :mobile 来渲染views/layout/application.mobile.haml
    猜你喜欢
    • 2018-11-11
    • 1970-01-01
    • 2021-07-25
    • 2020-03-28
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    相关资源
    最近更新 更多