Rails アプリに次のコードがあります。
link_to('???',{:controller => 'profile', :action => 'show', :id => id})
「???」をなくしたい 動的に生成された URL を表示します。どうすればいいですか?これはautolink gemの動作に似ていますが、Rails で URL オプションをテキストに変換してもらいたいのですが、その逆ではありません。
Rails アプリに次のコードがあります。
link_to('???',{:controller => 'profile', :action => 'show', :id => id})
「???」をなくしたい 動的に生成された URL を表示します。どうすればいいですか?これはautolink gemの動作に似ていますが、Rails で URL オプションをテキストに変換してもらいたいのですが、その逆ではありません。
link_to(nil,{:controller => 'profile', :action => 'show', :id => id})
nil が名前として渡された場合、リンク自体の値が名前になります
文字列を取得するために使用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)