Forum: Ruby on Rails Navigation menu

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.
F66cc63471b71843dba49db8e2b3771d?d=identicon&s=25 Lieven De keyzer (lieven)
on 2005-12-15 23:27
I have a navigation menu that contains a link per controller in my
application.
This menu is the same for all controllers, but I want this behaviour:

<a>Users</a>
<span>Statistics</span>

and

<span>Users</span>
<a>Statistics</a>

depending on the page you're currently on.

I've put a <render :partial => "_menu"> in the layout/application.rhtml
file that is used as layout for all views. And in every view map, I then
define a _menu.rhtml partial.
This is of course not flexible enough, since when I want to change some
name, I have to make changes in all these files.
What is a better way?
F0223b1193ecc3a935ce41a1edd72e42?d=identicon&s=25 zdennis (Guest)
on 2005-12-15 23:52
(Received via mailing list)
Lieven De Keyzer wrote:
> <a>Statistics</a>
>
> depending on the page you're currently on.
>
> I've put a <render :partial => "_menu"> in the layout/application.rhtml
> file that is used as layout for all views. And in every view map, I then
> define a _menu.rhtml partial.
> This is of course not flexible enough, since when I want to change some
> name, I have to make changes in all these files.
> What is a better way?
>

I don't know how you are getting your controller names, but you can use
a shared partial, and do
something like:

<% Dir['app/controllers/*.rb' ].each do |filename|
    controller_name = File.basename filename %>
   <% if @controller.controller_name == controller_name %>
      <span>%= controller_name %></span>
   <% else %>
      <a><%= controller_name %></a>
   <%end
end %>

@controller should be the controller that the request was made on,

Zach
55742d912a9003080d202c5522d4de00?d=identicon&s=25 Ken Bowley (Guest)
on 2005-12-16 00:04
(Received via mailing list)
You may want to try something like this:

<%= link_to_unless_current("Users", :action=>'users') %>
<%= link_to_unless_current("Statistics", :action=>'statistics' ) %>
F66cc63471b71843dba49db8e2b3771d?d=identicon&s=25 Lieven De keyzer (lieven)
on 2005-12-21 06:00
zdennis wrote:

> I don't know how you are getting your controller names, but you can use
> a shared partial, and do
> something like:
>
> <% Dir['app/controllers/*.rb' ].each do |filename|
>     controller_name = File.basename filename %>
>    <% if @controller.controller_name == controller_name %>
>       <span>%= controller_name %></span>
>    <% else %>
>       <a><%= controller_name %></a>
>    <%end
> end %>
>
> @controller should be the controller that the request was made on,
>
> Zach

But I might not want to create a link for each controller.
F66cc63471b71843dba49db8e2b3771d?d=identicon&s=25 Lieven De keyzer (lieven)
on 2005-12-21 06:05
Ken Bowley wrote:
> You may want to try something like this:
>
> <%= link_to_unless_current("Users", :action=>'users') %>
> <%= link_to_unless_current("Statistics", :action=>'statistics' ) %>

This won't work for me, because:

I want to have a menu on the page like:

<%= link_to 'Test1', :controller => 'test', :action => 'index' %>
<%= link_to 'Test2', :controller => 'test2', :action => 'index' %>

Then, When I click on the first link, the menu will still be on the
page, and above the content, there will be some other links, like this:

<%= link_to 'Edit', :controller => 'test', :action => 'edit' %>
<%= link_to 'New', :controller => 'test', :action => 'new' %>

But for all these pages, I still want the menu to not display a link but
a span for the current controller.

I hope you understood my explanation?
55742d912a9003080d202c5522d4de00?d=identicon&s=25 Ken Bowley (Guest)
on 2005-12-21 15:42
(Received via mailing list)
On Wed, 21 Dec 2005, Lieven De Keyzer wrote:

> <%= link_to 'Test1', :controller => 'test', :action => 'index' %>
>
> I hope you understood my explanation?

Look up information on link_to_if

Something similar to this might work for you:

<%= link_to_if @controller!='test', 'Test1', :controller => 'test',
:action => 'foo' %>
F66cc63471b71843dba49db8e2b3771d?d=identicon&s=25 Lieven De keyzer (lieven)
on 2005-12-21 18:18
Ken Bowley wrote:

> Look up information on link_to_if
>
> Something similar to this might work for you:
>
> <%= link_to_if @controller!='test', 'Test1', :controller => 'test',
> :action => 'foo' %>

Indeed,

<ul>
  <% controller_name = @controller.controller_name %>
  <li><%= link_to_if controller_name != 'test',
                     'Test',
                     :controller => 'test',
                     :action     => 'index' %>
  </li>
</ul>


did the trick.
One more question, I used the temporary variable controller_name because
I will need it in a lot of links. Should it be a regular variable or an
instance variable?
This topic is locked and can not be replied to.