简体   繁体   中英

Printing HTML tags into Ruby on Rails

I'm trying to print out an HTML table in Ruby on Rails

<h1>Welcome</h1>
<p>
<%=     sprintf print_series(@myTable) %>
</p>

*The Helper:

module WelcomeHelper
    def print_series(graph)     
        res = '<table border="2">'
        graph.each {
            |point|
            res = res + new_row(new_cell(point["date"]) + new_cell(point["value"]))
        }
        res << '</table>'
        return res.html_safe
    end
end

Nothing seems to work, I keep getting the HTML tags escaped, such that I can "see" the HTML on my browser, instead of seeing a table. What am I doing wrong?

Rails 3 automatically escapes html in output.

There are a number of ways to tell it not to do so for selected output.

Here's a Railscast about the subject

Added Drop the sprintf:

<%= print_series(@myTable) %>

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