带有 link_to...Ruby on Rails 的绝对 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5117345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:30:37  来源:igfitidea点击:

Absolute URL's with link_to...Ruby on Rails

ruby-on-rails

提问by Chris Muench

Is it possible to generate absolute URL's in rails using link to? [NOTE: THIS IS IN A MAILER]

是否可以使用链接在 rails 中生成绝对 URL?[注意:这是在邮件中]

I tried to do:

我试图做:

<%= link_to root_url, root_url%>

But I get a runtime error:

但我收到一个运行时错误:

*Missing host to link to! Please provide :host parameter or set default_url_options[:host]*

*缺少要链接的主机!请提供 :host 参数或设置 default_url_options[:host]*

I need this to be dynamic because the application will run on a wildcard domain (*.domain.com)

我需要它是动态的,因为应用程序将在通配符域 (*.domain.com) 上运行

采纳答案by Chris Muench

回答by Simone Carletti

If you use the _urlsuffix, the generated URL is absolute. Use _pathto get a relative URL.

如果使用_url后缀,则生成的 URL 是绝对的。使用_path获得相对URL。

<%= link_to "Home", root_url %>

<%= link_to "Home", root_path %>

回答by Simon Liu

Depending on your use case, string interpolation might be a good solution:

根据您的用例,字符串插值可能是一个很好的解决方案:

link_to(body, "http://#{site_url}")

回答by Awea

In routes.rb insert :

在 routes.rb 中插入:

root :to => 'controller#action'

Or replace your current map.root with the correct one.

或者用正确的替换你当前的 map.root。

See documentation about this : routes.rb usage

请参阅有关此的文档:routes.rb 用法