Search code examples
htmlruby-on-railsruby-on-rails-4meta-tagshtml-safe

Rails 4 - Meta tags html safe


I am trying to figure out the best way to make my meta tags html safe. They are currently escaping out any html.

Here is my current setup.

in my application.html.erb:

<meta name="description" content="<%= yield(:description) %>">

and in my views:

<% provide(:description, "Things being escaped from here") %>

if I simply call html safe on the provide like so...

<% provide(:description, "now it's html safe".html_safe) %>

it works fine, but I am wondering if there is a better practice in doing this. I find it wrong to have to call html safe on every single view where I am using the provide method.


Solution

  • You can create a custom helper based on the original provide.

    # app/helpers/application_helper.rb
    module ApplicationHelper
      def provide_safe(name, content)
        provide(name, content.html_safe)
      end
    end
    

    And in your views, just replace provide by provide_safe

    <% provide_safe(:description, "now it's html safe") %>