你可以将一个类添加到一个erb变量插值中吗?

huangapple go评论58阅读模式
英文:

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>

huangapple
  • 本文由 发表于 2023年5月18日 03:49:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定