wolfbagel
wolfbagel

Reputation: 319

Having trouble with Ruby on Rails 3 select_tag

I'm trying to hardcode a simple html select_tag but my option values are not rendering. I have a student table and want them to select a class group: Freshman, Sophomore, Junior, Senior. Here is my code in form.html.erb that I tried with just Freshman:

   Class Year <%= select_tag "class", "<option>Freshman</option>" %>

Any suggestions? Thanks

Upvotes: 0

Views: 1346

Answers (2)

Eric Hu
Eric Hu

Reputation: 18218

The option part was getting sanitized by rails 3. It seems like rails 2 didn't do this, so you were probably looking at documentation written for an older version of rails. This is how I got the options tag to render with your example

<%= select_tag "class", raw("<option>Freshman</option>") %>

Be careful about using this arbitrarily. raw tells rails to leave any string as it is when rendering HTML. If you're rendering dynamic content that users can modify (not the case here), you'll be vulnerable to SQL injection attacks.

Btw, the way I figured out what was amiss here was by using firebug (Firefox plugin) and viewing the rendered page. You can right click on page elements and inspect element to see the html. You can also do this with any browser's "view source" but I find this just a tad more convenient. This is what the html looked like with your original code:

<select id="class" name="class">&lt;option&gt;Freshman&lt;/option&gt;</select>

Upvotes: 2

bor1s
bor1s

Reputation: 4113

Look here select_tag and options_for_select

Upvotes: 1

Related Questions