简体   繁体   English

在Ruby on Rails URL助手中保留:format

[英]Keep :format in Ruby on Rails URL helpers

Suppose I use a custom :format to implement a gadget-oriented version of my site. 假设我使用自定义:format来实现我的网站的面向小工具的版本。 The general idea is that I can reuse controllers with custom, gadget-oriented views. 通常的想法是,我可以使用具有面向小工具的自定义视图的控制器重用。

Is there any way to make all URL helpers called from that particular format keep the same format, without hardcoding it into all helpers? 有什么方法可以使从该特定格式调用的所有URL帮助程序保持相同的格式,而无需将其硬编码为所有帮助程序?

I'd like to keep controllers untouched, and redirect_to calls from the controllers ignore the current format. 我想保持控制器不变,并且来自控制器的redirect_to调用将忽略当前格式。

nevermind, found an elegant solution: 没关系,找到了一个优雅的解决方案:

def default_url_options(options = nil)
  options ||= {}
  options[:format] = :gadget if request.format == :gadget
  options
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM