简体   繁体   中英

Absolute URL's with link_to…Ruby on Rails

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

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]*

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

If you use the _url suffix, the generated URL is absolute. Use _path to get a relative URL.

<%= link_to "Home", root_url %>

<%= link_to "Home", root_path %>

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

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

In routes.rb insert :

root :to => 'controller#action'

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

See documentation about this : routes.rb usage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM