Topic: How could I make this view code dryer?

<% unless current_page?(:controller => 'users', :action => 'edit') %>
<li><%= link_to "My Account", edit_user_path(1)%></li>
<%else%>
<li><%= link_to "<span>My Account</span>", edit_user_path(1), :class => 'selected' %></li>
<%end%>

Any tips appreciated. Thanks

Re: How could I make this view code dryer?

inn8 wrote:

<% unless current_page?(:controller => 'users', :action => 'edit') %>
<li><%= link_to "My Account", edit_user_path(1)%></li>
<%else%>
<li><%= link_to "<span>My Account</span>", edit_user_path(1), :class => 'selected' %></li>
<%end%>

Any tips appreciated. Thanks

<li><%= link_to "<span>My Account</span>", edit_user_path(1), :class => "#{current_page?(:controller => 'users', :action => 'edit') ? "selected" : ""}" %></li>
#
#

This works because of the additive nature of css classes:  setting class => "" is the same as not setting class at all.

I didn't do the bit with the spans as i didn't know why it's even there but you could apply the same technique.  Although it would then become less readable (and therefore worse, readability is better than dryness imo) than what you had to start off with.

Last edited by Max Williams (2008-12-18 07:59:02)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: How could I make this view code dryer?

Thanks Max

The spans are there for a bg image technique..I know the markup's a bit quirky

Thanks for the ideas!