views:

275

answers:

1

I don't get the point how can I do that code with an select helper?

<% @cube_names.each do |cube| %> " <% if @cube_name == cube %> selected="selected"<% end %>><%= cube %> <% end %>

I have a list (@cube_names) and want a HTML select box width all values of the list and the default value (param @cube_name) should be selected.

thanks.

+3  A: 

The select_tag helper will not auto-set the selected attribute on an item you pass. It just builds the tag. Use something like:

<%= select_tag("id_of_my_tag", @cube_names.map { |cn| "<option#{cn == cube ? " selected='selected'" : ""}>#{cn}</option>" }.join("")) %>

The first parameter is the id of the select tag, the second is a list of options (here built by mapping the cube names to strings, then joining the array into a single string).

You could alternatively use the options_for_select to build the string:

<%= select_tag("id_of_my_tag", options_for_select(@cube_names, cube)) %>
Sinan Taifour