Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - select_tag helper - array

I have a question I'm a little embarrassed to ask, but can't seem to figure out.

I'm writing a form to allow users to filter information for them to see only what they want. In the form I'm using the select_tag helper for a drop down menu. The selection menu is populated by an array of users. I have the following code:

<% @users.each do |user| %>
  <%= select_tag "users", options_for_select([user.name]) %> 
<% end %>

The problem with this, is it yields a selection menu for every user.name in the @users array. I assume the problem is that I'm using .each on my @users. However, I've been struggling with this too long this morning so I figured I'd just ask...

What's the proper way to get items from an array to populate into a single tag while using the select_tag helper?

Thanks

like image 867
Kombo Avatar asked Feb 25 '11 14:02

Kombo


1 Answers

You don't have to loop through the users. You can just do this:

<%= select_tag "users", options_from_collection_for_select(@users, "id", "name") %>
like image 158
Mischa Avatar answered Sep 27 '22 20:09

Mischa