2

So I was trying to display some font-awesome icons, when a favorite is true or either false.

I had something similar like below:

Application Controller

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

helper_method :favorite_text

And in the view:

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

2 Answers 2

4

You have to use the helper html_safe to signal that the hrml inside the string is safe and does not have to be escaped (simply speaking).

You can add this call inside your helper:

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

Then you can create a link by calling link_to with a block:

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

A more in-depth explanation can be found in the official docs: https://apidock.com/rails/String/html_safe

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for the late reply, but none of your suggestions worked.
By "not working" you mean your tags are still escaped?
I added an alternate call to output your text. Please have a look?
-1

I faced a similar issue to show ratings stars of fontawesome but figured out a way through the below helper method

###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)

Hope it helps.

2 Comments

Thank you, but the provided code isn't really related to my current problem. imo
Well..you can change the code in the way you want..it should get your work started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.