Controller paths

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!

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.

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.

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

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.

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

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.

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”.

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 :slight_smile:

Matt