Simone Melloni
Simone Melloni

Reputation: 430

Disable html escaping in a helper function

i have created in my application_helper a define for a standard link_to like this:

module ApplicationHelper
    def foo_link_to(text, path)
      out = "<span class=\"span\">"
      out += link_to text, path
      out += "</span>"
      out
    end
end

and in my partial i have

<%= foo_link_to 'text', home_path %>

but this is my output

&lt;span class=&quot;span&quot;&gt;&lt;a href=&quot;/home&quot;&gt;index&lt;/a&gt;&lt;/span&gt;

now, my question is: where i need to insert the html_escape?
ty all

Thanks at all for the support. Now i have another issue...if i wont this output what i have to do?

<a href="home.html"><i class="iclass"></i><span class="span"> text </span></a>

Using raw out and out.html_safe the output is

a href="/home">/home</a>&lt;span class=&quot;span&quot;&gt;&lt;i class=&quot;iclass&quot;&gt;text&lt;/i&gt;&lt;/span&gt;

Upvotes: 0

Views: 270

Answers (4)

Billy Chan
Billy Chan

Reputation: 24815

Use raw, in your last line

raw out

And your helper can be further refactored as

def foo_link_to(text, path)
  content_tag :span do
    link_to text, path
  end
end

I forget but it seems you don't need raw in the later case.

Update: For the last icon one, you can output like this

link_to 'home.html' do
  content_tag :i, class: 'iclass'
  content_tag :span, class: 'span' do
    'Text'
  end
end

Upvotes: 2

amit karsale
amit karsale

Reputation: 755

html_safe can help you:

use out.html_safe

you can refer this

Upvotes: 0

smoK
smoK

Reputation: 55

I think you should insert raw o html_safe in last line

Upvotes: 0

gotva
gotva

Reputation: 5998

I think it should be

module ApplicationHelper
  def foo_link_to(text, path)
    out = "<span class=\"span\">"
    out += link_to text, path
    out += "</span>"
    out.html_safe
  end
end

Upvotes: 1

Related Questions