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)
You should use request. original_url to get the current URL.
An absolute URL contains more information than a relative URL does. Relative URLs are more convenient because they are shorter and often more portable. However, you can use them only to reference links on the same server as the page that contains them.
An absolute URL is the full URL, including protocol ( http / https ), the optional subdomain (e.g. www ), domain ( example.com ), and path (which includes the directory and slug).
If you prefix the URL with // it will be treated as an absolute one. For example: <a href="//google.com">Google</a> . Keep in mind this will use the same protocol the page is being served with (e.g. if your page's URL is https://path/to/page the resulting URL will be https://google.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 %>
Depending on your use case, string interpolation might be a good solution:
link_to(body, "http://#{site_url}")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With