#69 Markaby in Helper

Do you ever need to generate HTML code in a helper method? Placing it directly in Ruby strings is not very pretty. Learn a great way to generate HTML through Markaby in this episode.
def simple_error_messages_for(object_name)
  object = instance_variable_get("@#{object_name}")
  return if object.errors.empty?
  
  markaby do
    div.error_messages! do
      h2 "#{pluralize(object.errors.count, 'error')} occurred"
      p "There were problems with the following fields:"
      ul do
        object.errors.each_full do |msg|
          li msg
        end
      end
    end
  end
end

def markaby(&block)
  Markaby::Builder.new({}, self, &block)
end

你可能感兴趣的:(html,Ruby)