rubyruby-on-rails-3nokogirihtml-safe

Undefined method html_safe for Nokogiri Html Document


I am trying to use .html_safe in the below description where I receive the error as Undefined method for Nokogiri HTML document.

blogs_controller.rb

@blog = Blog.find(19)
@description = Nokogiri::HTML.parse(@blog.description)
@description.search('a.fr-file').each do |desc|
  desc['href']= File.join(ActionController::Base.asset_host, desc['href'])
end

show.html.erb

<p><%= @description.html_safe %></p>

Kindly advise.


Solution

  • html_safe is a Rails method defined on String but not on Nokogiri::HTML.

    I would try to translate the Nokogiri document into a HTML first:

    <p><%= @description.to_html.html_safe %></p>