# Links on the left hand side - are components necessary?

**URL:** <https://www.ruby-forum.com/t/links-on-the-left-hand-side-are-components-necessary/62736>\
**Category:** Rails\
**Created:** [June 7, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/links-on-the-left-hand-side-are-components-necessary/62736 "2006-06-07T19:08:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [June 7, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/links-on-the-left-hand-side-are-components-necessary/62736/1 "2006-06-07T19:08:19Z")

</div>

Hello,

Maybe this is super-trivial…  
I need to have a list of links on the left hand side of my application,  
for _every_ single page.

So, I’ve created a layout called “main” (main.rhtml) under  
view/layouts, and included \>layout “main”\< in pretty all of my  
controllers.

main.rhtml has, amongst all the other HTML stuff:

\<%= render\_component :controller=\> ‘dli/extras’, :action=\>  
‘show\_lookup\_links’%\>

This is the component to display the links:

class Dli::ExtrasController \< ApplicationController  
uses\_component\_template\_root  
Link=Struct.new(:name,:controller,:action)

def show\_lookup\_links  
@links=[]  
@links \<\< Link.new(“Channels”, “/lookup/channel”, “index”)  
@links \<\< Link.new(“Password durations”,  
“/lookup/password\_duration”, “index”)  
render :layout=\>false  
end

end

Now: the list of links will need to change according to the user’s  
permission. So, there is quite a lot of logic involved here.

What are my options, if I don’t want to use components?

The only solution I can think of is to add a method to the general  
application controller (application.rb). However, how do call such an  
action from main.rhtml? It looks like I need to be able to specify  
controller _and_ action, which brings me back to components…

Can somebody please enlighten me? I feel that I am heading the wrong  
way, and…

Thanks,

Merc.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [June 7, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/links-on-the-left-hand-side-are-components-necessary/62736/2 "2006-06-07T19:48:56Z")

</div>

Components are going out of favour anyway so you’re best off with  
partials:

\<%= render :partial =\> ‘foo/bar’ %\>

then in app/views/foo/\_bar.rhtml you include your code. You can add the  
logic into a before\_filter method that creates an instance variable that  
the partial can access.

You could also create a helper method in  
app/helpers/application\_helper.rb:

def my\_helper(options = {})

# make code

end

Then you can just call it in your templates:  
\<%= my\_helper :option1 =\> ‘foo’, :option2 =\> ‘bar’ %\>

The first method is probably best though. If you’re generating a list  
of links, i would create a before\_filter in application\_controller that  
loads the links into a collection then pass this to the partial:

- application\_controller.rb  
before\_filter :load\_menu

def load\_menu

# load links into @links

end

- main.rhtml

 \<%= render :partial =\> 'link', :collection =\> @links %\>

- \_link.rhtml

- \<%= link %\>

Check out

> **[ActionController::Base](https://api.rubyonrails.org/classes/ActionController/Base.html#M000206)**
>
> Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed on request and then either it renders a template or redirects to another action.

for more information about partials.

Hope that helps,

Steve

Tony M. wrote:

> Hello,
> 
> Maybe this is super-trivial…  
> I need to have a list of links on the left hand side of my application,  
> for _every_ single page.
> 
> So, I’ve created a layout called “main” (main.rhtml) under  
> view/layouts, and included \>layout “main”\< in pretty all of my  
> controllers.
> 
> main.rhtml has, amongst all the other HTML stuff:
> 
> \<%= render\_component :controller=\> ‘dli/extras’, :action=\>  
> ‘show\_lookup\_links’%\>
> 
> This is the component to display the links:
> 
> class Dli::ExtrasController \< ApplicationController  
> uses\_component\_template\_root  
> Link=Struct.new(:name,:controller,:action)
> 
> def show\_lookup\_links  
> @links=[]  
> @links \<\< Link.new(“Channels”, “/lookup/channel”, “index”)  
> @links \<\< Link.new(“Password durations”,  
> “/lookup/password\_duration”, “index”)  
> render :layout=\>false  
> end
> 
> end
> 
> Now: the list of links will need to change according to the user’s  
> permission. So, there is quite a lot of logic involved here.
> 
> What are my options, if I don’t want to use components?
> 
> The only solution I can think of is to add a method to the general  
> application controller (application.rb). However, how do call such an  
> action from main.rhtml? It looks like I need to be able to specify  
> controller _and_ action, which brings me back to components…
> 
> Can somebody please enlighten me? I feel that I am heading the wrong  
> way, and…
> 
> Thanks,
> 
> Merc.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [June 7, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/links-on-the-left-hand-side-are-components-necessary/62736/3 "2006-06-07T19:54:36Z")

</div>

On 6/7/06, Tony M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def show\_lookup\_links  
> @links=[]  
> @links \<\< Link.new(“Channels”, “/lookup/channel”, “index”)  
> @links \<\< Link.new(“Password durations”,  
> “/lookup/password\_duration”, “index”)  
> render :layout=\>false  
> end

This looks like trouble. You’re going to be better off if you use  
link\_to() and let Rails build the paths that it needs. When I see  
“/lookup/channel” hardcoded it sets off warning bells.

> Can somebody please enlighten me? I feel that I am heading the wrong  
> way, and…

One thing that you could do to get away from using a component would  
be to put the common pieces of your nav as partials in  
/app/view/shared. Your layout(s) could then use this shared nav RHTML  
code as needed.

– James

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [June 7, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/links-on-the-left-hand-side-are-components-necessary/62736/4 "2006-06-07T20:01:05Z")

</div>

On Wednesday, June 07, 2006, at 7:48 PM, Stephen B. wrote:

> app/helpers/application\_helper.rb:  
> loads the links into a collection then pass this to the partial:  
> \<%= render :partial =\> ‘link’, :collection =\> @links %\>
> 
> > view/layouts, and included \>layout “main”\< in pretty all of my  
> > uses\_component\_template\_root  
> > end
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

This is basically how I have done it. A partial to display the links.  
In my case, I am using the user\_engine/login\_engine combo, so all I have  
to do is run a query against the permissions table to get a list of  
actions that are allowed. It’s not perfect as it is because there are  
several actions that you really wouldn’t want users to call directly  
(i.e., an ‘edit’ action needs an id to work).

Another approach would be to pre-generate a list of menu items and then  
use ‘link\_if\_authorized’ instead of ‘link\_to’. This will make them show  
up only if the user has permission to access them (it’s included in the  
user\_engine).

\_Kevin

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [June 7, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/links-on-the-left-hand-side-are-components-necessary/62736/5 "2006-06-07T19:57:48Z")

</div>

What’s wrong with just putting

\<%=link\_to\_unless\_current “Home”, :controller=\>“main”, :action=\>“index”  
%\>

in your main.rhtml file

If they’re always the same across all pages, why make the extra work?  
If you need them to be dynamically generated, then use render :partial  
as stated in an earlier post.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [June 8, 2006, 6:33am UTC](https://www.ruby-forum.com/t/links-on-the-left-hand-side-are-components-necessary/62736/6 "2006-06-08T06:33:14Z")

</div>

Hi,

> > def show\_lookup\_links  
> > @links=[]  
> > @links \<\< Link.new(“Channels”, “/lookup/channel”, “index”)  
> > @links \<\< Link.new(“Password durations”,  
> > “/lookup/password\_duration”, “index”)  
> > render :layout=\>false  
> > end
> 
> This looks like trouble. You’re going to be better off if you use  
> link\_to() and let Rails build the paths that it needs. When I see  
> “/lookup/channel” hardcoded it sets off warning bells.

Oh no, it’s not. /lookup/channel is _actually_ a contoller’s name which  
lives in a separate directory (I put a whole bunched of controllers  
under “lookup/…” and called them Lookup::ChannelController, for  
example).

Merc.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [June 8, 2006, 3:34am UTC](https://www.ruby-forum.com/t/links-on-the-left-hand-side-are-components-necessary/62736/7 "2006-06-08T03:34:17Z")

</div>

Hello,

Brian H. wrote:

> What’s wrong with just putting
> 
> \<%=link\_to\_unless\_current “Home”, :controller=\>“main”, :action=\>“index”  
> %\>
> 
> in your main.rhtml file
> 
> If they’re always the same across all pages, why make the extra work?

From my post:

“Now: the list of links will need to change according to the user’s  
permission. So, there is quite a lot of logic involved here.”

> If you need them to be dynamically generated, then use render :partial  
> as stated in an earlier post.

Yep - that’s exactly what I’m doing.

Merc.
