0

Rails アプリに次のコードがあります。

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

「???」をなくしたい 動的に生成された URL を表示します。どうすればいいですか?これはautolink gemの動作に似ていますが、Rails で URL オプションをテキストに変換してもらいたいのですが、その逆ではありません。

4

2 に答える 2

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

link_to ドキュメントから:

nil が名前として渡された場合、リンク自体の値が名前になります

于 2013-03-12T13:53:20.447 に答える
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 に答える