2017-07-12 37 views
0

我想创建一个标签,其中包含根路径加上和任意字符串的基础。从rails helper的任意link_to

我可以在控制器中使用ENV["ROOT_URL"]环境变量创建字符串,并将字符串传入,但我宁愿不要。

输出我想是

<a href="www.mysite.com/randomstring">Go To Link</a> 

# what i've tried 
link_to "Random string", root_path + "randomstring" %> 

我已经看过了link_to文档,但目前还不清楚如何建立任意URL

回答

1

URL是所有意图只是一堆文本。请记住,您必须以http://https://作为任何外部链接的前缀,否则它们将被解释为相对路径。

你的情况:

<a href="http://example.com/randomstring">Go To Link</a> 

变为:

= link_to('Go To Link', 'http://example.com/randomstring') 

或者更一般地:

= link_to('Go To Link', 'http://example.com/' + randomstring) 

哪里randomstring是你想,只要任何在它的文本变量它作为URL的一部分有效。

+0

是否有可能通过root_path获取环境根url并在其上添加字符串? – user2954587

+0

如果你愿意,你可以从'ENV'中抽取东西,或者为了那个事情而从其他地方抽取东西。 – tadman

+0

好吧,希望有一种方法不会,只是从root_path得到它 – user2954587