# Absolute urls in url\_for vs. link\_to

**URL:** <https://www.ruby-forum.com/t/absolute-urls-in-url-for-vs-link-to/120228>\
**Category:** Rails\
**Created:** [November 14, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/absolute-urls-in-url-for-vs-link-to/120228 "2007-11-14T23:09:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 14, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/absolute-urls-in-url-for-vs-link-to/120228/1 "2007-11-14T23:09:37Z")

</div>

In both url\_for and link\_to, the :only\_path param can be used to  
determine whether the link url used is complete with hostname and path,  
or just has the path (a kind of relative url).

However, in url\_for it defaults to false (urls with hostnames), and in  
link\_to it defaults to true ( urls without hostnames, just paths).

Is there any reason for this discrepency? To me, it violates the  
principle of least surprise.

Is there any way for me to make Rails default to false for link\_to also?  
I have a case where the urls without hostnames are messing something up,  
and I really don’t want to have to go in and add :only\_path to every  
single call to link\_to. I really want to be able to change the default  
behavior globally. Anyone know if there’s a good way to do that?

Thanks!

Jonathan

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 14, 2007, 11:29pm UTC](https://www.ruby-forum.com/t/absolute-urls-in-url-for-vs-link-to/120228/2 "2007-11-14T23:29:30Z")

</div>

> Is there any way for me to make Rails default to false for link\_to also?  
> I have a case where the urls without hostnames are messing something up,  
> and I really don’t want to have to go in and add :only\_path to every  
> single call to link\_to. I really want to be able to change the default  
> behavior globally. Anyone know if there’s a good way to do that?

Something like this (untested, but this is pretty close):

module ActionView  
module Helpers  
module UrlHelper

```
   alias_method :link_to_orig :link_to

   def link_to(name, options = {}, html_options = nil, 

```

\*parameters\_for\_method\_reference)  
link\_to(name, {:only\_path =\> false}.merge(options),  
html\_options, paramaters\_for\_method\_reference)  
end

```
 end

```

end  
end

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 14, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/absolute-urls-in-url-for-vs-link-to/120228/3 "2007-11-14T23:45:52Z")

</div>

> > Is there any way for me to make Rails default to false for link\_to also?
> 
> ```
> alias_method :link_to_orig :link_to
> 
> def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference)
> link_to(name, {:only_path => false}.merge(options), html_options, paramaters_for_method_reference)
> 
> ```

Bah! the above “link\_to” should be “link\_to\_orig”. Heh.

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 15, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/absolute-urls-in-url-for-vs-link-to/120228/4 "2007-11-15T16:59:47Z")

</div>

Cool, thanks. That got me thinking. What I actually ended up doing  
instead is over-ride url\_for in application\_helper, to respect a @ivar.

def url\_for(options={}, \*parameters\_for\_method\_reference)  
options[:only\_path] = false if @urls\_with\_hostname &&  
options[:only\_path].nil?  
super(options, parameters\_for\_method\_reference)  
end

After I thought of this, this seems to work nicely. Defaults both  
url\_for and link\_to in views to :only\_path=false (:url\_for in controller  
already defaults to this, go figure), can be controlled on an  
action-by-action (or even block-by-block) basis, still respects  
:only\_path passed in manually in call if present.

Is there any reason not to do what I’m doing there?

Jonathan

Philip H. wrote:

> > > Is there any way for me to make Rails default to false for link\_to also?
> > 
> > ```
> > alias_method :link_to_orig :link_to
> > 
> > def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference)
> > link_to(name, {:only_path => false}.merge(options), html_options, paramaters_for_method_reference)
> > 
> > ```
> 
> Bah! the above “link\_to” should be “link\_to\_orig”. Heh.
