1

I have lines of HTML I want to place throughout the Rails view, but I only want the line of code to originate from a single source and not be copied throughout.

Example:

I have the following code I want to put in multiple places:

<i class="bi-trophy-fill" style="font-size: 1.25rem; color: gold;"> 1st Place</i>

So I tried creating a helper method that stores the html as a string:

 def place_symbol(place)
    if place == 1
        string = %(<i class="bi-trophy-fill" style="font-size: 1.25rem; color: gold;"> 1st Place</i>)
    elsif place == 2
        string = %(<i class="bi-trophy-fill" style="font-size: 1.0rem; color: silver;"> 2nd Place</i>)
    elsif place == 3
        string = %(<i class="bi-trophy-fill" style="font-size: 1.0rem; color: #CD7f32;"> 3rd Place</i>)  
    end
    
    return string
end

and in my view I have:

    <%= "#{place_symbol(1)}" %>

But it just prints out the string into the view rather than recognizing it as HTML. Is there a rails method where it would know its meant to be html code and not actually printed out?

railsnoob
  • 51
  • 5
  • 1
    some of the answers here might help answer your question: https://stackoverflow.com/questions/4576932/ruby-on-rails-how-to-render-a-string-as-html although the more Rails approach might be to use a partial – benmercerdev Oct 18 '22 at 02:00
  • the <%== place_symbol(1) %> works great, which was in that answer. thank you! – railsnoob Oct 19 '22 at 02:34

0 Answers0