因此,当收藏夹为true或false时,我试图显示一些字体真棒的图标。
我有类似如下的内容:
应用程序控制器
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 %>
答案 0 :(得分:1)
您必须使用辅助程序Id Clientid Branchid
1 1 1
1 1 1
来表示字符串中的hrml是安全的,不必进行转义(简而言之)。
您可以在助手中添加此呼叫:
html_safe
然后,您可以通过调用带有块的def favorite_text
(@favorite_exists ? '<i class="fas fa-heart"></i>' : '<i class="fas fa-heart-o"</i>').html_safe
end
来创建链接:
link_to
更深入的解释可以在官方文档中找到:https://apidock.com/rails/String/html_safe
答案 1 :(得分:0)
我遇到了类似的问题,要显示丰满的评级明星,但想出了一种通过以下帮助方法的方法
###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)
希望有帮助。