简体   繁体   中英

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>

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

Use raw or html_safe

raw

<%= raw linkify_page(@page) %>

html_safe

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM