레일의 래퍼 방법을 통해 선택적 인수를 전달합니다
-
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>
신분증이 나타나지 않는 이유는 무엇입니까?
해결책
Theiv의 제안 덕분에 작동하는 버전을 찾았습니다.
def link_to_with_current(text, link, condition, *args)
options = args.first || {}
options[:class] = condition ? 'current' : nil
link_to text, link, options
end
제휴하지 않습니다 StackOverflow