<% if I18n.locale == :pl %>
<%= link_to 'PL', { :locale => :pl }, :class => "language_selected" %> | <%= link_to 'ENG', { :locale => :en } %>
<% else %>
<%= link_to 'PL', { :locale => :pl } %> | <%= link_to 'ENG', { :locale => :en }, :class => "language_selected" %>
<% end %>
Refactorings
No refactoring yet !
sfusion
August 25, 2010, August 25, 2010 11:29, permalink
why not plonk it in a helper?
def locale_links
[:pl, :en].collect |locale|
class = (I18n.locale == locale) ? 'language_selected' : nil
link_to locale.to_s.upcase, { :locale => locale }, :class => class
end.join(' | ')
end
Martin Plöger
August 27, 2010, August 27, 2010 14:35, permalink
I'd also put it in a helper (putting logic directly in the view isn't very nice to maintain). I would not set the html_options-hash with an empty class though... IMHO
BTW: if/unless expression can be put in parentheses and used as a parameter in method calls. Also have a look at the link_to_if and link_to_unless methods...
def locale_links
[:pl, :en].map { |locale| link_to locale.to_s.upcase, {:locale => locale}, (:class => :language_selected if I18n.locale == locale)}.join ' | '
end
Omer Jakobinsky
August 30, 2010, August 30, 2010 09:12, permalink
Thanks both of you I really like the one liner and I would definitely have a look at link_to_if.
Hi, I'm sure this is the most horrible way I could ever do it but I just cant figure out how to insert an if statement next to a link_to class.