Ruby-on-rails – Drop down box in Rails


How do I use Rails to create a drop-down selection box? Say if I have done the query:

@roles = Role.all

Then how do I display a box with all the's ?

EDIT: After implementing the dropdown box. How do I make it respond to selections? Should I make a form?

Best Solution

use the collection_select helper

This will allow you to write something like:

collection_select(:user, :role_id, @roles, :id, :role_title, {:prompt => true})

And get

<select name="user[role_id]">
  <option value="">Please select</option>
  <option value="1" selected="selected">Administrator</option>
  <option value="2">User</option>
  <option value="3">Editor</option>