5

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

1 Answer 1

8

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") %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.