【问题标题】:Adding extension to URL: Rails向 URL 添加扩展名:Rails
【发布时间】:2010-12-14 17:12:25
【问题描述】:

我正在 Rails 应用程序中设置一些 link_to xml 视图。 url如何显示.xml扩展名?

Need it to appear as:
http://localhost:3000/test/1-testing.xml

Currently it appears as:
http://localhost:3000/test/1-testing

【问题讨论】:

    标签: ruby-on-rails url hyperlink


    【解决方案1】:

    在 Rails 3 中,假设你的路径是 foo_path,那么你想要:

    foo_path(:format=>:xml)
    

    link_to,你可以这样做

    link_to "link text", foo_path(:format => :xml)
    

    还有更多选择:

    link_to "link text", foo_path(:format => :xml), :id=>"foo_id", :class=>"foo_class"
    

    (这个问题太老了,但我想我会回答帮助任何通过谷歌找到这个问题的人,就像我一样。)

    【讨论】:

      【解决方案2】:

      假设你想链接到实例@test,试试:

      test_url(@test, :format => :xml)
      

      【讨论】:

      猜你喜欢
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多