AnApprentice
AnApprentice

Reputation: 110950

Rails 3 Select Box helper, how to add custom options at the bottom that are static

How can I create a select box that has the following given 4 results for User.departments:

Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
-----
Add New Department

Based on the following models:

User.department_id
Departments (id, title, abbreviation)

What I can't figure out is how add the two options at the bottom that say Add New Department.

Here is what I have so far:

<%= collection_select(:user, :department_id, Department.where(:id => current_user.department_id), :id, :title, {:prompt => true})  %>

Thanks

Upvotes: 1

Views: 480

Answers (1)

Donavan White
Donavan White

Reputation: 1126

Your best bet is to use select instead of collection_select for this, here is an example of how I would do it.

<%= f.select(:user, Department.where(:id => current_user.department_id).collect {|p| [[p.title,' (', p.abbreviation,')'], p.id] } + ['Add New Department']) %>

Then you could use something like javascript to do something when 'Add New Department' got selected or however you planned on using it.

Hope this helps and happy coding.

Upvotes: 1

Related Questions