Search code examples
htmlruby-on-railserbgenerate

Generate HTML in ERB Template View


I need to add options to a HTML-selector combobox inside an ERB template. First I create an array named var_arr with <option value="foo">Foo</option>-items out of an array with items/values like foo and then I try to write it between the <select></select>-tags with each

<% var_arr.each do |option| %>
  <%= option %> 
<% end %>

What it generates is

<option value="article">Article</option> <option value="link">Link</option> <option value="photo">Photo</option> <option value="treenode">TreeNode</option> 

But it's not treated as HTML. The options aren't displayed as options in the dropdown menu.

Where's the hidden caveat here?

Yours

von Spotz


Solution

  • ERB treats the text from variables as text and not as html you need to mark the options as html_safe to treat it as html like this.

    <% var_arr.each do |option| %>
      <%= option.html_safe %>
    <% end %>