0

我的 Rails 应用程序中有以下代码:

link_to('???',{:controller => 'profile', :action => 'show', :id => id})

我想摆脱'???' 并显示动态生成的 URL。我该怎么做?这有点像自动链接 gem所做的,但我希望 Rails 将 URL 选项转换为文本,反之亦然。

4

2 回答 2

1

用于url_for()获取字符串。

url_for({:controller => 'profile', :action => 'show', :id => id)}

在代码中:

url_hash = {:controller => 'profile', :action => 'show', :id => id}
link_to(url_for(url_hash), url_hash)

请参阅此问题以获取主机名:

url_hash = {:controller => 'profile', :action => 'show', :id => id}
link_to("#{request.host_with_port}#{url_for(url_hash)}", url_hash)
于 2013-03-12T11:56:02.823 回答
1
link_to(nil,{:controller => 'profile', :action => 'show', :id => id})

link_to 文档

如果将 nil 作为名称传递,则链接本身的值将成为名称

于 2013-03-12T13:53:20.447 回答