Rails反向自动链接

时间:2013-03-12 11:52:34

标签: ruby-on-rails ruby-on-rails-3

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

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

我想摆脱'???'它显示动态生成的URL。我怎么这样?它有点像autolink gem那样,但我希望Rails将URL选项转换为文本,反之亦然。

2 个答案:

答案 0 :(得分: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)

请参阅this question以获取主机名:

url_hash = {:controller => 'profile', :action => 'show', :id => id}
link_to("#{request.host_with_port}#{url_for(url_hash)}", url_hash)

答案 1 :(得分:1)

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

来自link_to documentation

  

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