Why is String Interpolation Failing in erb Template?

11,485

Because strings delimited with single quotation marks '' are not interpolated.

Change your code to e.g.:

<% embed = "<a href=\"http://someurl.com/whatever\">#{@webcast.name}</a>" %>

or (if you want to avoid masking the double-quote characters ")

<% embed = %(<a href="http://someurl.com/whatever">#{@webcast.name}</a>) %>

or just (thanks to Samy Dindane for the hint):

<% embed = "<a href='http://someurl.com/whatever'>#{@webcast.name}</a>" %>
Share:
11,485
Undistraction
Author by

Undistraction

Updated on June 19, 2022

Comments

  • Undistraction
    Undistraction almost 2 years

    I have the following code in a .erb file:

    <% embed='<a href="http://someurl.com/whatever">#{@webcast.name}</a>'%>
    
    <p id="embedCode">
        <pre>
            <code>
                <%= embed %>
            </code>
        </pre>
    </p>
    

    The anchor tag is correctly displayed onscreen as text rather than rendered as a dom element, however the string interpolation is failing. The html is successfully displayed as text but #{@webcast.name} is not evaluated. If a include <%= @webcast.name => in the template, the webcast name in rendered successfully.

  • Samy Dindane
    Samy Dindane almost 12 years
    You can use ' as the href delimiter. I find this prettier. :)