Rails - How to convert path to URL in views?

3.3k views Asked by At

As title, in my mailer views, I need a helper to convert pathes to URLs:

= link_to "Click Me", convert_to_urlh#with_hash")

I expected convert_to_url("/some_path#with_hash") to generate http://localhost:3000/some_path#with_hash.

2

There are 2 answers

0
Felix Clack On

You don't need to call url_for here as the link_to method already calls it with the second argument. Instead try this:

link_to 'Click Me', '/some_path#with_hash'
0
AlkH On

If you want to get URL in ActionMailer, then you need to specify :host. You can use plain url_for, polymorphic_url or generated named routes methods.

For example:

<%= link_to 'Click me', user_url(@user, host: "example.com") %>

In views and controllers Rails substitute :host automagically

More information: