Ruby Building Web Apps with Sinatra Finishing Touches Summary

Rack::Utils.escape_html(string) does not work as described in the teacher's notes

When adding the following to my wiki.rb file:

def escape(string)
  Rack::Utils.escape_html(string)
end

And adding the following in my show.erb file:

<p><%= escape @content %></p>

When adding the following content when creating a new page:

<script>alert('boo');</script>

My results were the following:

%3Cscript%3Ealert%28%27boo%27%29%3B%3C%2Fscript%3E

Shouldn't this have rendered as <script>alert('boo');</script>, but with the HTML entities escaped?

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Hmm, that's true! Another method (that's meant for escaping special characters in URLs) is overriding the escape method.

Looks like the fix is just to re-name the method we add to wiki.rb. I went with a method name of h (as in "HTML"):

def h(string)
  Rack::Utils.escape_html(string)
end

I've updated the teacher's notes to reflect this. Sorry for the confusion!