我有一個方法song_link
,它在內部調用link_to
。我希望調用者能夠將選項散列傳遞給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]
是否存在,如果它不將其用於song_link
的目的,而無需將其傳遞到link_to
顯然,這種做法不會因爲我添加了更多與song_link
相關的選項,但不包含link_to
。我應該怎麼做?
那麼基本上做我在做什麼? – 2009-10-30 02:38:10
這是要麼通過兩個不同的選項哈希。 – 2009-10-30 03:44:59