0

I have the following code in my Rails app:

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

I want to get rid of '???' and it to show the dynamically generated URL. How do I this? It's kind of like what the autolink gem does, but I want Rails to convert URL options into text, not vice versa.

Maarten
  • 6,894
  • 7
  • 55
  • 90

2 Answers2

1

Use url_for() to get the string.

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

In the code:

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

See this question to also get the host name:

url_hash = {:controller => 'profile', :action => 'show', :id => id}
link_to("#{request.host_with_port}#{url_for(url_hash)}", url_hash)
Community
  • 1
  • 1
Maarten
  • 6,894
  • 7
  • 55
  • 90
1
link_to(nil,{:controller => 'profile', :action => 'show', :id => id})

From the link_to documentation:

If nil is passed as the name the value of the link itself will become the name

Gareth
  • 133,157
  • 36
  • 148
  • 157