添加URL扩展:Rails

Jef*_*rey 7 url ruby-on-rails hyperlink

我在rails应用程序中设置了一些link_to xml视图.网址如何显示.xml扩展名?

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

Currently it appears as:
http://localhost:3000/test/1-testing
Run Code Online (Sandbox Code Playgroud)

Gra*_*ier 13

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

foo_path(:format=>:xml)
Run Code Online (Sandbox Code Playgroud)

在一个link_to,你可以做到

link_to "link text", foo_path(:format => :xml)
Run Code Online (Sandbox Code Playgroud)

还有更多选择:

link_to "link text", foo_path(:format => :xml), :id=>"foo_id", :class=>"foo_class"
Run Code Online (Sandbox Code Playgroud)

(这个问题很老了,但我想我会回答帮助那些通过Google找到这个问题的人,就像我做的那样.)


ry.*_*ry. 2

假设您想链接到实例 @test,请尝试:

test_url(@test, :format => :xml)
Run Code Online (Sandbox Code Playgroud)