Completing an if-else statement in content_tag for Rails

How do I wrap an if-else statement in content_tag?

def entry_template
    content_tag(:div, :class => "item_title") do
      "<h3>


       if x == 'garage'
         #{entry.garage.name}
       else
         'Not garage'
       end


       </h3>"
    end
end

Thank!

+3
source share
1 answer
def entry_template
    content_tag(:div, :class => "item_title") do
      "<h3>" +
       if x == 'garage'
         entry.garage.name
       else
         'Not garage'
       end +
       "</h3>"
    end
end

or, in my eyes, a little nicer, because he doesn’t switch styles all the time,

def entry_template
    content_tag(:div, :class => "item_title") do
      content_tag(:h3) do
        if x == 'garage'
          entry.garage.name
        else
          'Not garage'
        end
      end
    end
end
+1
source

Source: https://habr.com/ru/post/1772055/


All Articles