تمرير الحجج الاختيارية من خلال طريقة التفاف في القضبان

StackOverflow https://stackoverflow.com/questions/2218911

  •  19-09-2019
  •  | 
  •  

سؤال

لدي طريقة التفاف التالية ل 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>

لماذا لا تظهر المعرف؟

هل كانت مفيدة؟

المحلول

بفضل اقتراح ثيفيف، وجدت نسخة تعمل:

def link_to_with_current(text, link, condition, *args)
  options = args.first || {}
  options[:class] = condition ? 'current' : nil
  link_to text, link, options
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top