1

所以我试图显示一些字体很棒的图标,当最喜欢的是真或假时。

我有类似下面的东西:

应用控制器

def favorite_text
    @favorite_exists ? '<i class="fas fa-heart"></i>' : '<i class="fas fa-heart-o"</i>'
end

helper_method :favorite_text

在视图中:

<%= link_to favorite_text, update_favorites_path, remote: true %>
4

2 回答 2

1

您必须使用帮助html_safe器来表示字符串中的 hrml 是安全的,并且不必转义(简单地说)。

您可以在助手中添加此调用:

def favorite_text
    (@favorite_exists ? '<i class="fas fa-heart"></i>' : '<i class="fas fa-heart-o"</i>').html_safe
end

link_to然后您可以通过使用块调用来创建链接:

<%= link_to update_favorites_path, remote: true do %>
    <%= favorite_text %>
<% end %>

更深入的解释可以在官方文档中找到:https ://apidock.com/rails/String/html_safe

于 2018-11-03T10:20:39.450 回答
0

我遇到了类似的问题来显示 fontawesome 的收视率明星,但通过下面的辅助方法找到了一种方法

###in my application_helper.rb

    def show_big_rating_stars(count)
    if count && count > 0
       (0...count).map { content_tag(:span, "" , class: "fa fa-star gold fa-2x") }.join  
    end    


    ###this is how use it in my views
    show_big_rating_stars(vendor.ratings.count)

希望能帮助到你。

于 2018-11-03T14:13:52.933 回答