# Controller paths

**URL:** <https://www.ruby-forum.com/t/controller-paths/57724>\
**Category:** Rails\
**Created:** [April 13, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/controller-paths/57724 "2006-04-13T14:31:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 13, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/controller-paths/57724/1 "2006-04-13T14:31:48Z")

</div>

Hi there.

Suppose I create some controllers like

ruby script/generate controller Admin::product add remove  
ruby script/generate controller Admin::user add remove  
ruby script/generate controller Login login logout

the directory structure will be

app/controllers/admin/product\_controller.rb  
app/controllers/admin/user\_controller.rb  
app/controllers/login\_controller.rb

Now suppose I need some links with link\_to from the product and user  
controller to the login controller. Does I need to use some paths like

link\_to “Logout”, :controller =\> “…/login”, :action =\> “logout”

or

link\_to “Logout”, :controller =\> “/login”, :action =\> “logout”

or is there a better way to do that?

Thanks!

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 13, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/controller-paths/57724/2 "2006-04-13T16:30:04Z")

</div>

In my experience you need to use:

link\_to “Logout”, :controller =\> “admin/login”, :action =\> “logout”

There may be another way to do it, but I haven’t found it yet.

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 13, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/controller-paths/57724/3 "2006-04-13T16:38:02Z")

</div>

Hi Eden!

> In my experience you need to use:  
> link\_to “Logout”, :controller =\> “admin/login”, :action =\> “logout”

On this case, I’m creating a link from admin to login. Admin is _under_  
login, so I need to refer to a level up there. If I use “admin/login” I  
think it will try to refers to

app/controllers/admin/login\_controller.rb

and not to

app/controllers/login\_controller.rb

that is the correct path, one level above admin.

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 13, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/controller-paths/57724/4 "2006-04-13T17:00:54Z")

</div>

The problem I’ve had is that if you don’t absolutely path the controller  
(  
i.e. link\_to “Logout”, :controller =\> “/admin/login”, :action =\>  
“logout”),  
it won’t work if you’re using “link\_to” within any of the “admin”  
controllers. It will prepend an extra “admin” to the link, so you’ll end  
up  
with “/admin/admin/login”.

If you’re linking to the login controller within one of the admin  
controllers, you can just use :controller =\> “/login”, :action =\>  
“logout”.

Matt

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 13, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/controller-paths/57724/5 "2006-04-13T16:46:22Z")

</div>

Hi again.

> On this case, I’m creating a link from admin to login. Admin is _under_  
> login, so I need to refer to a level up there.

Sorry, I made some confusion here. What I’d like to say is login is  
_above_ login, so I need to refer to a level up the admin controller.  
So, from the admin/product controller, for example, create a link to the  
login controller, one level up.

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 13, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/controller-paths/57724/6 "2006-04-13T17:10:16Z")

</div>

Yes, but it’s my understanding that link\_to uses url\_for to create the  
link,  
and it will be relative to the current controller. It’s not smart enough  
to  
figure out that you have a login\_controller that’s above the current  
directory path. Instead it makes the link relative to the current  
controller. If you’re in a sub-controller (or whatever you want to call  
it),  
this will be incorrect.

If someone knows better, _please_ enlighten me, because this does indeed  
seem to be a bit non-intuative.

Matt

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 13, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/controller-paths/57724/7 "2006-04-13T17:04:00Z")

</div>

Hmmm. Well, I am still new to all of this, but the basic rule of thumb  
I  
use is that :controller should be set as if you are always in the  
controllers folder regardless of where the call is coming from. Rails  
routing should take care of determining the exact html link.

So I _think_ that:

link\_to “Logout”, :controller =\> “login”, :action =\> “logout”

should work even if you are calling it from views/admin/product. I  
would be  
interested in hearing if this assumption is wrong since I haven’t tried  
this  
exact combination yet.

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 13, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/controller-paths/57724/8 "2006-04-13T18:25:54Z")

</div>

Hi!

Seems that if I use

link\_to “Logout”, :controller =\> “/login”, :action =\> “logout”  
link\_to “Logout”, :controller =\> “/admin/user”, :action =\> “add”

it works ok. The first / works as, let me call this way, the “controller  
root”.

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [April 13, 2006, 10:37pm UTC](https://www.ruby-forum.com/t/controller-paths/57724/9 "2006-04-13T22:37:31Z")

</div>

Yeah, that’s basically what I was referring to… I picked it up off of  
a  
blog post, but I’ve since lost track of where I found it. I was in the  
same  
situation myself yesterday 🙂

Matt
