Metaphysiker
Metaphysiker

Reputation: 1093

How to show html of link_to helper in rails

Here is my link_to helper:

<%= link_to pdf.title, pdf.file.url(:original, false), target: :_blank  %>

This produces the following html:

<a target="_blank" href="/system/pdfs/files/000/000/005/original/cv.pdf">my pdf file!</a>

How can I print the same html in view?

What I tried:

  1. Method

    <%= debug (link_to pdf.title, pdf.file.url(:original, false), target: :_blank) %>

This produces the correct html. However, the html is displayed within a grey box. enter image description here

I just need the string. I don't want the grey box, the dots or the hyphens. Further, someone noted that debug might not work in production.

  1. Method

       <%= (link_to pdf.title, pdf.file.url(:original, false), target:
      :_blank).inspect  %>
    

This produces faulty html. As you can see, characters are escaped:

"<a target=\"_blank\" href=\"/system/pdfs/files/000/000/005/original/cv.pdf\">CV</a>" 

Any suggestions?

Upvotes: 1

Views: 258

Answers (1)

Amin Shah Gilani
Amin Shah Gilani

Reputation: 9826

You don't need debug, you can just convert it into a string. For example:

<%= "#{link_to 'Google', 'https://google.com', target: '_blank'}" %>

This will output the following to the page:

&lt;a target=&quot;_blank&quot; href=&quot;https://google.com&quot;&gt;Google&lt;/a&gt;

Upvotes: 1

Related Questions