【发布时间】:2012-01-25 06:53:59
【问题描述】:
我有两种方法可以做类似的事情。我是一个菜鸟,想知道如何将这些组合成一个方法:
#test if the current selected language is the one that was clicked in the menu
def link_to_without_class_unless_current_language(language)
if language_selected?(language)
content_tag(:li, content_tag(:span, content_tag(:em, language)), :class => "current")
else
content_tag :li, link_to(content_tag(:span, language), :locale => language.prefix)
end
end
#tests if the current page is the same as that for the link
def link_to_without_class_unless_current(name, options)
if current_page?(options[:url])
content_tag(:li, content_tag(:span, content_tag(:em, name)),
:class => options[:class] ||= "current")
else
content_tag :li, link_to(content_tag(:span, name), options[:url])
end
end
在视图中:
<%= link_to_without_class_unless_current_language 'English' %> |
<%= link_to_without_class_unless_current_language 'Français' %>
<%= link_to_without_class_unless_current t('application.menu_links.home'),
{ :url => root_url } %>
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 refactoring dry