Pregunta

I have a navbar, which is showing an unread messages count. The unread count has to go within the navbar text for formatting, so I have used the below:

<li><%= link_to "My Messages #{current_user.mailbox.receipts.where({:is_read => false}).count(:id, :distinct => true).to_s}", conversations_path, class: 'small button' %></li>

How do I add a class to the count? I want to add the bootstrap navbar-unread class.

I can put:

<span class='navbar-nav'><%= current_user.mailbox.receipts.where(:is_read => false).count(:id, :distinct => true).to_s %></span> 

...outside the list but then the unread count is not in the right place.

Thanks for any help, can't find an answer anywhere, or at least a better way to do this.

EDITED:

The output I need from the html would look like this:

<li>
    <a class="small button" href="/converssations/link">
    My Messages<span class="navbar-unread">0</span>
    </a>
</li>

So I need to pass a class into a span around the ruby that starts 'current_user.mailbox...etc

It's to show the number of unread messages, which is a small number in the corner of the button.

¿Fue útil?

Solución

Maybe this can help you.

Look at ActionView Helpers reference also.

<%= link_to content_tag(:span, "My Messages #{current_user.mailbox.receipts.where({:is_read => false}).count(:id, :distinct => true).to_s}", class: "navbar-nav"), conversations_path, class: "small button" %>

will have output like this

<a class="small button" href="/converssations/link">
    <span class="navbar-nav">My Messages Count</span>
</a>

To achieve your mentioned behavior you can pass a block to link_to like this

<%= link_to "#your_link", class: "small button" do %>
My Message <%= content_tag(:span, "1", class: "navbar-unread") %> 
<% end %>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top