Forum: Ruby on Rails Calling absolute links

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
26ca684555abbdb44f01bd1e165e649b?d=identicon&s=25 Vince W. (vince)
on 2005-12-12 04:06
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?
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2005-12-12 04:57
(Received via mailing list)
Why don't you add a :controller option to the link like so:

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

Kent.
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2005-12-12 05:00
(Received via mailing list)
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
4353600bbb6b6e0950e62f7f44e49dd5?d=identicon&s=25 Steven Smith (Guest)
on 2005-12-12 05:09
(Received via mailing list)
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.
Cf7e5e4b5ca573eec214191fac420a2f?d=identicon&s=25 Dave Silvester (Guest)
on 2005-12-12 05:12
(Received via mailing list)
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" -%>
<div id="sidebar">
  <%= link_to("Items", :action => "list") %>
</div>
<% 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" -%>
<div id="sidebar">
  <%= link_to("Items", :action => "list") %>
</div>
<% end -%>

Untested, but either should do what you want.  HTH!  :-)

~Dave

--

Dave Silvester
Rent-A-Monkey Website Development
Web: http://www.rentamonkey.com/
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2005-12-12 05:27
(Received via mailing list)
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.
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2005-12-12 05:33
(Received via mailing list)
Isn't it that this extra / should be added to the controller name, not
action
name?

Kent.
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2005-12-12 06:30
(Received via mailing list)
Possibly...  I was typing from memory while I'm at work.  Sorri if I've
lead
anyone down the garden path...

Cheers
This topic is locked and can not be replied to.