2009-10-30 4 views
1

link_to을 내부적으로 호출하는 song_link 메서드가 있습니다. 호출자가 옵션 해시를 song_link에 전달하면 관련 옵션을 사용하고 나머지는 link_to으로 전달합니다. 여기에 내 코드입니다 :선택적으로 다른 방법으로 옵션 전달

def song_link(song, separator = nil, options = {}) 
    if separator.class == Hash 
     options = separator 
     separator = nil # not sure about this logic either! 
         # I guess I should roll it into the options hash 
    end 

    primary_only = false 
    if options[:primary_only] 
     options.delete(:primary_only) 
     primary_only = true 
    end 

    link_to title_with_artists(song, separator, primary_only), song_path(:song_slug => song.song_slug, :artist_slug => song.artist_slug), options 
    end 

즉, 나는 options[:primary_only]이 있는지 여부를 확인하려면, 그것은 분명히 link_to

이 방법을 따라 전달하지 않고 song_link의 목적으로 사용 않는 경우는하지 않습니다 규모가 song_link과 관련이 있지만 link_to과 관련된 옵션을 더 추가하면됩니다. 어떻게해야합니까?

답변

6

단순화 도우미 :

def song_link(song, options = {}) 
    separator = options.delete(:separator) 
    primary_only = options.delete(:primary_only) 

    name = title_with_artists(song, separator, primary_only) 
    path = song_path(:song_slug => song.song_slug, :artist_slug => song.artist_slug) 
    link_to name, path, options 
end 

nil 그냥 false만큼 좋은이며, 다른 모든 true만큼 좋은 사실을 활용.

1

전달하기 전에 해시에서 처리하는 옵션을 제거하십시오.

+0

그래서 기본적으로 내가하고있는 일을합니까? –

+0

두 가지 옵션 해시를 전달하거나 전달합니다. –

관련 문제