英文:
Can I add a class to a erb variable interpolation?
问题
我有一个列表项,里面有一个名为“Open”的link_to,里面嵌入了一个名为@open的变量,它是打开评论的计数。我想把从@open变量获取的数据/数字放进一个 Bootstrap 徽章中,使用的类是class="badge badge-pill badge-primary"。
这是我正在努力解决的代码行。
<li id="open" class="nav-item <%= 'active' if params[:q][:status_eq] == "open" %>">
<%= link_to "Open #{@open}", comments_path(q: {status_eq: "open"}), class: 'nav-link' %>
</li>
英文:
I have a list item with a link_to inside and a name of "Open" and a interpolated variable called @open which is a count of open comments . I am wanting to put the data/number only that I get from the @open variable inside a bootstrap badge using class="badge badge-pill badge-primary".
This is my line of code that I am trying to work on.
<li id="open" class="nav-item <%= 'active' if params[:q][:status_eq] == "open" %>">
<%= link_to "Open #{@open}", comments_path(q: {status_eq: "open"}), class: 'nav-link' %>
</li>
I have tried to change the class on the erb from nav-link to badge badge-pill badge-primary but it puts the word "open" inside the badge.
I tried strange forms of interpolation and adding class="" or class: or class=> to the variable directly like "Open #{@open class: "badge badge-pill badge-primary"}" with no luck.
答案1
得分: 1
If I understand correctly, you want @open count to be wrapped in the Bootstrap badge and display Open as text only.
你希望@open的计数被包裹在Bootstrap的徽章中,只显示Open作为文本。
英文:
If I understand correctly, you want @open count to be wrapped in the Bootstrap badge and display Open as text only.
You can use link_to as a block and break out the HTML inside the link.
<li id="open" class="nav-item <%= 'active' if params[:q][:status_eq] == "open" %>">
<%= link_to comments_path(q: {status_eq: "open"}), class: 'nav-link' do %>
Open <span class="badge badge-pill badge-primary"><%= @open %></span>
<% end %>
</li>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论