ZK Zhao
ZK Zhao

Reputation: 21583

Rails: add addtional html into content_tag

In my navigation, I need '.active' to tell user if they are in the current page. So I defined a helper to generate li.active dynamically.

def nav_link(link_text, link_path
  class_name = current_page?(link_path) ? 'active' : nil
  content_tag(:li, :class => class_name) do
    link_to link_text, link_path
  end
end

so it can generate

<li class="active">
<a href="/resources">Resources</a>
</li>

However, for one specific link, I want to add <strong> to the <a> inside <li> to make it stands out from others. How can I do it?

<li class="active">
<strong><a href="/resources">Resources</a></strong>
</li>

UPDATE: From this

enter image description here

To this

enter image description here

Every nav link(e.g. new,resources, videos) all uses the nav_link helper. What I need, is a way to insert some html(or CSS) into the resouces link. To make it stands out.

Upvotes: 0

Views: 761

Answers (1)

vee
vee

Reputation: 38645

Could you not make the change in css?

.active {
  font-weight: bold;
}

You can certainly nest content_tag to add strong node:

def nav_link(link_text, link_path, additional_class = nil)
  class_name = current_page?(link_path) ? 'active' : nil
  content_tag(:li, :class => class_name) do
    content_tag(:strong) do 
      link_to link_text, link_path
    end
  end
end

Update:

You could check for "Resources" as follows to add the strong tag:

def nav_link(link_text, link_path, additional_class = nil)
  class_name = current_page?(link_path) ? 'active' : nil
  content_tag(:li, :class => class_name) do
    link = link_to(link_text, link_path)

    if link_text == 'Resources' 
      content_tag(:strong) { link }
    else
      link
    end
  end
end

Upvotes: 1

Related Questions