Calling absolute links


#1

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?


#2

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

<%= link_to("Items", :controller => 'primary', :action => "list") %>

Kent.


#3

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


#4

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


#5

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

~Dave

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


#6

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.


#7

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

Kent.


#8

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

Cheers