2010-02-08 10 views
1

我有link_to以下包裝方法:通過一個包裝方法中的Rails傳遞可選參數

def link_to_with_current(text, link, condition, *args) 
    current_class = condition ? 'current' : nil 
    link_to text, link, :class => current_class, *args 
end 

當用該樣品稱爲:

link_to_with_current 'My Link', '/mylink.html', true, :id => 'mylink' 

以下鏈接生成:

<a href="/mylink" class="current">My Link</a> 

爲什麼不顯示ID?

+1

要使用這些參數的圖示,我不認爲,我想你會更好使用哈希(例如作爲'options = {}')。查看'link_to'的源代碼http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001597 – theIV

回答

1

由於theIV的建議下,我找到了一個版本的作品:

def link_to_with_current(text, link, condition, *args) 
    options = args.first || {} 
    options[:class] = condition ? 'current' : nil 
    link_to text, link, options 
end 
相關問題