加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在Rails中,如何制作:移动视图在未找到时回退

发布时间:2020-12-17 02:17:59 所属栏目:百科 来源:网络整理
导读:这个问题是我之前未回答的问题的后续问题: ActionView::MissingTemplate: Missing template (Trying to render nonexistent :mobile format ) 由于似乎没有与Rails方法达成共识,有什么方法可以从移动设备访问以呈现默认值:html何时:移动格式不可用? (如
这个问题是我之前未回答的问题的后续问题: ActionView::MissingTemplate: Missing template (Trying to render nonexistent :mobile format )

由于似乎没有与Rails方法达成共识,有什么方法可以从移动设备访问以呈现默认值:html何时:移动格式不可用? (如果a:移动视图存在,则应优先于未移动格式的移动视图).

解决方法

假设你有一个mobile_request?控制器实例方法来检测移动请求,那么你应该能够设置格式回退链:

# application_controller.rb
before_filter :set_request_format,:set_format_fallbacks

respond_to :html,:mobile # etc

def set_request_format
  request.format = :mobile if mobile_request?
end

def set_format_fallbacks
  if request.format == :mobile
    self.formats = [:mobile,:html]
  end
end

这应该工作,但显然它并不完全.
https://github.com/rails/rails/issues/3855
如果你有一个移动模板,格式似乎被锁定,它将找不到只有html的部分.

希望它会以某种方式修复.
在此期间,您可以将此<%controller.set_format_fallbacks%>在每个模板(ouch)或编写自己的解析器.
http://jkfill.com/2011/03/11/implementing-a-rails-3-view-resolver/

还看看:

Can a mobile mime type fall back to “html” in Rails?

Changing view formats in rails 3.1 (delivering mobile html formats,fallback on normal html)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读