I’m trying to use a select_tag helper in Rails 3. I started with a very basic example, copied straight from the documentation:
It seems to produce the correct markup, but the select doesn’t work – clicking it does nothing.
For comparison, I created the same select in HAML. That works fine. Here is the code for both:
-# The select_tag version
= select_tag "count", "<option>1</option><option>2</option><option>3</option><option>4</option>"
-# The HAML version
%select{:name => "count", :id => "count"}
%option 1
%option 2
%option 3
%option 4
The select_tag seems to produce the options in a string, but not as DOM elements – in Firebug, they are just gray, not syntax-highlighted like the DOM elements in the working select produced by HAML.
What’s the deal?
I figured out the answer just before I posted the question: the string needs to have .html_safe called on it.
Viewing source shows that Rails has turned all the
<and>into<and>, because it’s now escaping strings by default.html_safesays “no really, trust me, this one is OK to display as HTML.”So this makes it work:
It would be nice if they updated the documentation, but hey, at least the answer is quickly searchable here now. 🙂