Search code examples
htmlruby-on-railserblinkify

Linkify Method in html.erb file showing up with HTML in view


sorry if this is a noob question but im calling the following method on the @page instance variable, and the view is populating with html tags...

module PagesHelper
  def linkify_page page
    regex = /\[\[([^|]+)\|(\w+)\]\]/
    page.text.gsub(regex) do |link|
      caps = regex.match(link)
      linked_page = page.adventure.pages.where(:name => caps[2]).first
      link_to(caps[1], adventure_page_path(page.adventure_id, linked_page.id))
    end
  end
end

show.html.erb

<%= linkify_page(@page) %>

Result in the view...

I can't wait to <a href="/adventures/1/pages/2">see the end</a>

Solution

  • You html_safe to escape the HTML tags

    <%= linkify_page(@page).html_safe %>
    

    OR

    use raw

    <%= raw linkify_page(@page) %>
    

    OR

    Just use <%== which is equivalent of raw

    <%== linkify_page(@page) %>
    

    For more details,see these Guides