Передача необязательных аргументов через метод-оболочку в Rails - PullRequest
1 голос
/ 08 февраля 2010

У меня есть следующий метод оболочки для link_to:

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>

Почему не отображается идентификатор?

1 Ответ

1 голос
/ 08 февраля 2010

Благодаря предложению theIV я нашел версию, которая работает:

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