I want to dynamically change the menu options in the application
layout based on the contents of the @session[:user] object. If the
user is logged in, then add ''logout'' option to the menu. If
the
logged in user is ''joe'', then add an ''admin''
link, as well. This is
the code I''m using:
<%= if @session[:user] then
link_to '' | LogOut'', :controller =>
''login'', :action => ''logout''
if @session[:user] == ''joe'' then
link_to '' | Admin'', :controller ==>
''admin'',
:action => ''index''
end
end %>
The problem: when joe logs in, the menu only shows the
''Admin'' link.
The ''logout'' link is not shown. How do I modify this so that
joe sees
both menu options?
-Larry
On 6/20/05, Larry Kelly <ldk2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The problem: when joe logs in, the menu only shows the ''Admin'' link. > The ''logout'' link is not shown. How do I modify this so that joe sees > both menu options?you have to be careful with the difference between "<%=" and "<%". "<%=" outputs the result of the expression and the result of multiple statements is the last statement executed (basically). Try this (untested): <% if @session[:user] %> <%= link_to '' | LogOut'', :controller => ''login'', :action => ''logout'' %> <% if @session[:user] == ''joe'' %> <%= link_to '' | Admin'', :controller ==> ''admin'', :action => ''index'' %> <% end %> <% end %> Sebastian
> <%= if @session[:user] then > link_to '' | LogOut'', :controller => ''login'', :action > => ''logout'' > if @session[:user] == ''joe'' then > link_to '' | Admin'', :controller ==> ''admin'', > :action => ''index'' > end > end %> > > The problem: when joe logs in, the menu only shows the ''Admin'' link. > The ''logout'' link is not shown. How do I modify this so that joe sees > both menu options?The <%= will only return the last value as a string. Try this: <% if @session[:user] %> <%= link_to '' | LogOut'', :controller => ''login'', :action => ''logout'' %> <% if @session[:user] == ''joe'' %> <%= link_to '' | Admin'', :controller ==> ''admin'', :action => ''index'' %> <% end %> <% end %> Alternatively, you can use ''concat'': <% if @session[:user] then concat link_to('' | LogOut'', :controller => ''login'', :action => ''logout''), binding if @session[:user] == ''joe'' then concat link_to('' | Admin'', :controller ==> ''admin'', :action => ''index''), binding end end %> Duane Johnson (canadaduane)