0

我使用的代码来自: http ://scottwb.com/blog/2012/02/23/a-better-way-to-add-mobile-pages-to-a-rails-site/

def check_for_mobile
    session[:mobile_override] = params[:_mobile] if params[:_mobile]
    prepare_for_mobile if mobile_device?
end

def prepare_for_mobile
    prepend_view_path Rails.root + 'app' + 'views_mobile'
end

def mobile_device?
    if session[:mobile_override]
      session[:mobile_override] == "1"
    else
      # Season this regexp to taste. I prefer to treat iPad as non-_mobile.
      (request.user_agent =~ /Mobile|webOS/) && (request.user_agent !~ /iPad/)
    end
end
helper_method :mobile_device?

这很棒并且工作正常,但是我将如何更改视图名称而不是视图路径?这是个人喜好,但我宁愿拥有这样的移动视图:

/app/views/users/index_mobile.html.erb 与 /app/views_mobile/users/index.html.erb

谢谢!

4

1 回答 1

1

使用render方法:

render is_mobile? ? :index_mobile : :index

请参阅2.2.2 渲染操作的视图

我怀疑是否可以渲染存储在/app/views. 即使有可能,这也是不明智的,因为它违反了 Rails 的路径约定。

于 2013-06-05T22:15:53.927 回答