# Calling absolute links

**URL:** <https://www.ruby-forum.com/t/calling-absolute-links/50017>\
**Category:** Rails\
**Created:** [December 12, 2005, 4:06am UTC](https://www.ruby-forum.com/t/calling-absolute-links/50017 "2005-12-12T04:06:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [December 12, 2005, 4:06am UTC](https://www.ruby-forum.com/t/calling-absolute-links/50017/1 "2005-12-12T04:06:51Z")

</div>

I have a primary layout that I’m using in several controllers. On my  
sidebar I have a link that I set up as follows:

```
<div id="sidebar">
    <%= link_to("Items", :action => "list") %>
</div>

```

Everything is ok when I call it from my primary controller, but since I  
am also calling the layout from a second controller that does not have  
list defined I am getting an error.

What I’d like to do is make the action =\> “list” absolute so that it  
goes to action =\> controller “primary” =\>“list”

Is there any way to do that? Or else do I just have to define list a  
second time in the second controller?

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [December 12, 2005, 4:57am UTC](https://www.ruby-forum.com/t/calling-absolute-links/50017/2 "2005-12-12T04:57:28Z")

</div>

Why don’t you add a :controller option to the link like so:

 \<%= link\_to("Items", :controller =\> 'primary', :action =\> "list") %\>

Kent.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [December 12, 2005, 5:00am UTC](https://www.ruby-forum.com/t/calling-absolute-links/50017/3 "2005-12-12T05:00:30Z")

</div>

Vince,

The way that I’ve gotten around this one is to use something like

\<%= link\_to(“Items”, :action =\> “/list”) %\>

That way your always calling the right action. Alternatively you could  
use  
\<%= link\_to(“Items”, :controller=\>“Item”, :action =\> “list” %\>

but I think you still need to put the / in there.

Cheers

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [December 12, 2005, 5:09am UTC](https://www.ruby-forum.com/t/calling-absolute-links/50017/4 "2005-12-12T05:09:30Z")

</div>

There are a few ways to make shared components like this work. One  
was already mentioned in that you could just specify a certain  
controller on the link\_to. You could also define sidebar as a shared  
component which would live at app\>views\>shared and include it where  
you’d like via \<%= render(:partial =\> “shared/sidebar”) %\>.  
Additionally, you could make it a component in the components  
directory (same level as app). A good start info-wise is here:  
[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/Components). You’ll still  
need to specify what controller / action you want in each of the  
sidebar links, but this follows the DRY principal quite nicely by  
locating the sidebar in one location.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [December 12, 2005, 5:12am UTC](https://www.ruby-forum.com/t/calling-absolute-links/50017/5 "2005-12-12T05:12:34Z")

</div>

On Monday 12 Dec 2005 03:06, Vince W. wrote:

> Everything is ok when I call it from my primary controller, but since I  
> am also calling the layout from a second controller that does not have  
> list defined I am getting an error.  
> What I’d like to do is make the action =\> “list” absolute so that it  
> goes to action =\> controller “primary” =\>“list”  
> Is there any way to do that? Or else do I just have to define list a  
> second time in the second controller?

Perhaps just make it conditional in your layout (change the name to the  
one  
where you don’t want it visible, assuming it should be visible in all  
others):

\<% unless controller.controller\_name ==  
“the\_one\_where\_you\_want\_it\_hidden” -%\>

 \<%= link\_to("Items", :action =\> "list") %\>
\<% end -%\>

Or alternatively, if you only want to show it in the primary one (again,  
change the name to whatever the only one you do want it visible is):

\<% if controller.controller\_name == “primary” -%\>

 \<%= link\_to("Items", :action =\> "list") %\>
\<% end -%\>

Untested, but either should do what you want. HTH! 🙂

~Dave

–

Dave S.  
Rent-A-Monkey Website Development  
Web: [http://www.rentamonkey.com/](http://www.rentamonkey.com/)

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [December 12, 2005, 5:27am UTC](https://www.ruby-forum.com/t/calling-absolute-links/50017/6 "2005-12-12T05:27:36Z")

</div>

Kent,

I’ve found in some circumstances say a User controller, and an  
Admin::User  
controller,  
that rails can get confused. since the User and Admin::User are really  
both  
User Controllers. That’s why I’ve added the extra / in the action.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [December 12, 2005, 5:33am UTC](https://www.ruby-forum.com/t/calling-absolute-links/50017/7 "2005-12-12T05:33:37Z")

</div>

Isn’t it that this extra / should be added to the controller name, not  
action  
name?

Kent.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [December 12, 2005, 6:30am UTC](https://www.ruby-forum.com/t/calling-absolute-links/50017/8 "2005-12-12T06:30:42Z")

</div>

Possibly… I was typing from memory while I’m at work. Sorri if I’ve  
lead  
anyone down the garden path…

Cheers
