【问题标题】:Rails default_url_options for actioncontroller to auto format all urls/paths with .html用于操作控制器的 Rails default_url_options 使用 .html 自动格式化所有 url/路径
【发布时间】:2010-11-19 17:07:36
【问题描述】:

我们的要求之一是我们所有的 url 都以 .html 结尾 我们重写了 default_url_options 方法以将格式添加到选项中

def default_url_options(options={})
  options.merge(:format => 'html')
end

这在大多数情况下都很好用... 但它会导致以下路线出现问题:

map.home '/', :controller => 'home'
map.root :controller => 'home'

它会导致这些路由返回:

domain.com/?format=html

我需要找到一种方法来对这些路线进行例外处理,这可能吗?或者有谁知道更聪明的方法来做到这一点。

【问题讨论】:

    标签: ruby-on-rails url-rewriting friendly-url actioncontroller


    【解决方案1】:

    这使它与haml一起工作......(未经测试的erb)

    map.home '/', :controller => 'home', :format => 'html'
    

    【讨论】:

      【解决方案2】:

      这在快速测试中似乎对我有用。

      map.home '/', :controller => 'home', :format => ''
      

      【讨论】:

      • 在 Rails 2.3.2 上实现这个给了我.... 处理 ApplicationController#index (for 127.0.0.1 at 2009-08-17 09:50:45) [GET] 参数:{" format"=>"", "action"=>"index", "controller"=>"home"} ArgumentError (interning empty string): /opt/local/lib/ruby/gems/1.8/gems/haml-edge -2.1.12/lib/haml/helpers/action_view_mods.rb:14:in `render'
      • 是的,我没有尝试使用haml - 抱歉。
      • 别担心,一定是haml挂钩到render的方式有关
      猜你喜欢
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 2012-06-19
      • 2017-02-17
      • 1970-01-01
      相关资源
      最近更新 更多