2012-01-25 4 views
1

私は同様のことをする2つの方法があります。私はnoobのだと私は1つの方法にこれらを組み合わせて作ることができるかもしれない方法を知りたい:ビューでこのコードはDRYしたいが問題がある

#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 } %> 

答えて

1

私は、あなたは、単にオプションのコレクションを渡すことを示唆している可能性があります代わりに、オプション[:url]があるかどうかを確認する2番目のメソッドの2番目のパラメータと同様に、options [:language]が設定されているかどうかを確認してから、最初のメソッドのコードでコードを実行し、 ]が2番目のメソッドのコードを実行するように設定されている場合は、名前も同様にオプションを指定することもできます。

ので、あなたの3つの呼び出しが、私はそれは私が何をするかだと思います。この

<%= link_to_without_class_unless_current :language => 'English' %> | 
<%= link_to_without_class_unless_current :language => 'Français' %> 

<%= link_to_without_class_unless_current :name => t('application.menu_links.home'), 
             :url => root_url %> 
+0

おかげでアンドリューのようになります。ありがとうございました。 – chell

関連する問題