# Link\_To best practices ( newbie Q)

**URL:** <https://www.ruby-forum.com/t/link-to-best-practices-newbie-q/114019>\
**Category:** Rails\
**Created:** [September 7, 2007, 5:16am UTC](https://www.ruby-forum.com/t/link-to-best-practices-newbie-q/114019 "2007-09-07T05:16:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [September 7, 2007, 5:16am UTC](https://www.ruby-forum.com/t/link-to-best-practices-newbie-q/114019/1 "2007-09-07T05:16:48Z")

</div>

I’ve seen a few examples of how us the link\_to and I’ve read the API  
documentation.

It seems that the API docs would recommend that when using this  
method, the best practice would be to write it as follows:

\<%= link\_to “This is FooBar”, { :controller =\> “foo”, :action =\>  
“bar”} %\>

But outside the API documentation, I’ve seen the method called as  
follows…

\<%= link\_to( ‘this is foobar’, :controller =\> ‘foo’, :action =\> ‘bar’)  
%\>

I prefer the second way because it seems more readable, but since the  
API docs don’t describe calling the method like this I’m wondering if  
it’s unsupported.

Is there a best practice for how to call this method? Why?

Thanks,  
Pat

---

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [September 7, 2007, 7:19am UTC](https://www.ruby-forum.com/t/link-to-best-practices-newbie-q/114019/2 "2007-09-07T07:19:57Z")

</div>

Hi Pat,  
The answer to your wonderment isn’t particular to Rails but is a  
function of Ruby’s flexibility in passing in arguments to a method.  
The link\_to method has four formal arguments, the first of which is  
required. The second and third both have default values (which the  
assume in absence of the prescence of a second and third argument,  
respectively) and the fourth represents a multi-valued argument where  
any amount of arguments greater than the first three are collected  
into an array and passed as the last argument.

As for the answer to your question, I bet there is a best practice  
because Rails is typically opinionated software. My guess would be  
that it is the first way which is best practice because it is a common  
Rails idiom and, IMO, more sincere to the intent of the arguments.

Obviously, the second way works but looking at the source code, I’m  
not exactly sure why it works. I’m going to have to step through with  
a debugger so let me say thanks for the interesting question.

After stepping through the debugger, I see that in your second  
example, the second two arguments (:controller =\> “foo”, :action =\>  
“bar”) are considered part of one hash and are passed to url\_for as  
the first argument(in this file  
[http://api.rubyonrails.org/files/vendor/rails/actionpack/lib/action\_view/helpers/url\_helper\_rb.html](http://api.rubyonrails.org/files/vendor/rails/actionpack/lib/action_view/helpers/url_helper_rb.html)

- NOTE: the link didn’t display source code for me but at least points  
you in the correct file for the source code, your local install  
location of the source code may vary).

Maybe someone with better understanding of ruby than I can explain why  
the second two arguments are considered part of one has as opposed to  
two? (I would assume its something to do with the binding priority  
of ‘=\>’ and its subsequent effects? I wouldn’t think a symbol is the  
trigger. Or is it that both strings and controllers objects have an  
operator (method) ‘=\>’ which takes in an argument and returns a hash  
object? How would I verify my supposition? Is there a ruby compiler  
with ruby source code? Or do I go try to grok C code?)]

Thanks,  
Jonathan

---

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [September 7, 2007, 7:23am UTC](https://www.ruby-forum.com/t/link-to-best-practices-newbie-q/114019/3 "2007-09-07T07:23:57Z")

</div>

Hi Pat,  
The answer to your wonderment isn’t particular to Rails but is a  
function of Ruby’s flexibility in passing in arguments to a method.  
The link\_to method has four formal arguments, the first of which is  
required. The second and third both have default values (which they  
assume in absence of the prescence of a second and third argument,  
respectively) and the fourth represents a multi-valued argument where  
any amount of arguments greater than the first three are collected  
into an array and passed as the last argument.

As for the answer to your question, I bet there is a best practice  
because Rails is typically opinionated software. My guess would be  
that it is the first way which is best practice because it is a common  
Rails idiom and, IMO, more sincere to the intent of the arguments.

Obviously, the second way works but looking at the source code, I’m  
not exactly sure why it works. I’m going to have to step through with  
a debugger so let me say thanks for the interesting question.

After stepping through the debugger, I see that in your second  
example, the second two arguments (:controller =\> “foo”, :action =\>  
“bar”) are considered part of one hash and are passed to url\_for as  
the first argument(in this file  
[http://api.rubyonrails.org/files/vendor/rails/actionpack/lib/action\_v](http://api.rubyonrails.org/files/vendor/rails/actionpack/lib/action_v)…

- NOTE: the link didn’t display source code for me but at least points  
you in the correct file for the source code, your local install  
location of the source code may vary).

Maybe someone with better understanding of ruby than I can explain why  
the second two arguments are considered part of one hash as opposed to  
two one element hashes? (I would assume its something to do with the  
binding priority  
of ‘=\>’ and its subsequent effects? I wouldn’t think a symbol is the  
trigger. Or is it that both strings and symbol objects have an  
operator (method) ‘=\>’ which takes in an argument/expression and  
returns a hash  
object? How would I verify my supposition? Is there a ruby compiler  
with ruby source code? Or do I go try to grok C code?)]

Thanks,  
Jonathan

---

<div class="post-metadata">

**Author:** ![pat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8491ac/32.png) [@pat](https://www.ruby-forum.com/u/pat)\
**Post date:** [September 7, 2007, 11:00am UTC](https://www.ruby-forum.com/t/link-to-best-practices-newbie-q/114019/4 "2007-09-07T11:00:19Z")

</div>

On Sep 7, 6:22 am, Jonathan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> As for the answer to your question, I bet there is a best practice  
> “bar”) are considered part of one hash and are passed to url\_for as  
> trigger. Or is it that both strings and symbol objects have an
> 
> > I’ve seen a few examples of how us the link\_to and I’ve read the API
> 
> > Pat  
> > Hi guys,

Ruby allows you to omit the brackets when passing a hash as an  
argument to a method. This is only allowed when the hash is the last  
argument to the method.

This is why the following two snippets are different, and will have  
different effects:  
\<%= link\_to “This is FooBar”, :controller =\> “foo”, :action =\>“bar”  
%\>  
\<%= link\_to “This is FooBar”, { :controller =\> “foo”}, {:action  
=\>“bar”} %\>

Cheers,  
Louis.
