# Question about link\_to

**URL:** <https://www.ruby-forum.com/t/question-about-link-to/210394>\
**Category:** Rails\
**Created:** [August 6, 2011, 6:46pm UTC](https://www.ruby-forum.com/t/question-about-link-to/210394 "2011-08-06T18:46:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [August 6, 2011, 6:46pm UTC](https://www.ruby-forum.com/t/question-about-link-to/210394/1 "2011-08-06T18:46:35Z")

</div>

Hi all

I am working on the first application from the book “Head First Rails”  
called “tickets”. What I do not understand is where the variable  
“new\_ticket\_path” gets populated in the view index.html.erb

\<%= link\_to ‘New Ticket’, new\_ticket\_path %\>

Please advise

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [August 6, 2011, 6:59pm UTC](https://www.ruby-forum.com/t/question-about-link-to/210394/2 "2011-08-06T18:59:44Z")

</div>

On Aug 6, 2011, at 12:46 PM, Pepe S. wrote:

> Please advise

This path is made up by the rails router. If you have  
‘resources :tickets’ in your routes file, then this path is generated  
automatically for you. Type rake routes in your Terminal to see the  
whole lot of them.

There’s an excellent Rails Guide about this, called Routing from the  
Outside In.

Walter

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [August 6, 2011, 7:22pm UTC](https://www.ruby-forum.com/t/question-about-link-to/210394/3 "2011-08-06T19:22:00Z")

</div>

Walter,

So if I understand correctly new\_ticket\_path, edit\_ticket\_path, ticket  
are like constants that RoR assigns to them specific values.

If I want to create a new link on my RoR Application to a new page  
called for example “myblog”, and add it to the index page, which will be  
the right syntax?

link\_to “myblog”, ???

Thanks  
jose

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [August 7, 2011, 4:31pm UTC](https://www.ruby-forum.com/t/question-about-link-to/210394/4 "2011-08-07T16:31:09Z")

</div>

I understand now!

To generate a link in my RoR application I just need to prefix the word  
“\_path” with a controller name and it finds it.

Thanks

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [August 6, 2011, 7:34pm UTC](https://www.ruby-forum.com/t/question-about-link-to/210394/5 "2011-08-06T19:34:22Z")

</div>

On Aug 6, 2011, at 1:22 PM, Pepe S. wrote:

> link\_to “myblog”, ???  
> That depends on where the blog is coming from. If it is part of your  
> Rails application, and you are linking to it from a different part of  
> the same Rails application, then you might be able to do something like:

link\_to “My Blog”, posts\_path

Which would generate a link like this: [My Blog](http://localhost:3000/posts)

That’s assuming that your blog is rendered by the #index method of the  
PostsController. Substitute your own controller name as needed.

If you have two separate Rails applications, and you want to link from  
one to the other, then you can either use a hand-written link, or you  
can use the :url attribute in the link\_to method. Read up on the  
link\_to generator in the API docs:

> **[link\_to(name = nil, options = nil, html\_options = nil, &block) Link -...](https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)**
>
> Creates an anchor element of the given name using a URL created by the set of options. See the valid options in the documentation for url\_for. It’s also possible to pass a String instead of an options hash, which generates an anchor element that uses...

Walter

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [August 7, 2011, 4:42pm UTC](https://www.ruby-forum.com/t/question-about-link-to/210394/6 "2011-08-07T16:42:13Z")

</div>

On 7 August 2011 15:31, Pepe S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I understand now!
> 
> To generate a link in my RoR application I just need to prefix the word  
> “\_path” with a controller name and it finds it.

That is only part of it. Did you read the Rails Guide on routing, as  
Walter suggested. This is absolutely required reading (and  
understanding).

Colin

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [August 7, 2011, 7:33pm UTC](https://www.ruby-forum.com/t/question-about-link-to/210394/7 "2011-08-07T19:33:02Z")

</div>

Pepe S. wrote in post #1015368:

> 

First of all, I think this:

> \<%= link\_to ‘New Ticket’, new\_ticket\_path %\>

should say: new\_tickets\_path. Note the plural form of tickets. At  
least that is the way it is in Rails 3.09. Or maybe, tickets\_path??  
Head First Rails is an old enough book that it’s probably not worth  
studying. 2008??! That’s a  
lifetime ago in computer programming, and rails changes faster than most  
things in the programming world.

> I understand now!
> 
> To generate a link in my RoR application I just need to prefix the word  
> “\_path” with a controller name and it finds it.

That’s not necessarily true. You were on the right track to begin with.  
The second argument to the link\_to method can be a path. Rails will  
create some paths for you and assigns names to them, like tickets\_path,  
which you can use as the second argument to link\_to.  
Rails also automatically creates some methods you can use:

ticket\_path(@ticket)

which creates the path to a page that shows a particular ticket. But if  
one of those ‘constants’ or methods actually produces the path:

‘/some\_page’

You could just specify that path directly yourself:

\<%= link\_to ‘New Ticket’, ‘/somepage’ %\>

The bottom line is: as long as your routes file maps a path to one of  
your actions, it doesn’t matter how you obtain the path:

1. using one of the variables rails provides for you, e.g. tickets\_path
2. using a method rails provides, e.g. ticket\_path(@ticket)
3. specifying the path directly yourself, e.g. ‘/somepage’
