Ruby on rails Rails 3 Select Box helper,如何在底部添加静态的自定义选项

Ruby on rails Rails 3 Select Box helper,如何在底部添加静态的自定义选项,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,如何为User.departments创建具有以下给定4个结果的选择框: Department.title (Department.abbreviation) Department.title (Department.abbreviation) Department.title (Department.abbreviation) Department.title (Department.abbreviation) ----- Add New Department 基于以下模型: User.de

如何为User.departments创建具有以下给定4个结果的选择框:

Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
Department.title (Department.abbreviation)
-----
Add New Department
基于以下模型:

User.department_id
Departments (id, title, abbreviation)
我搞不懂的是如何添加底部的两个选项,即添加新部门

以下是我到目前为止的情况:

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

谢谢

您的最佳选择是使用select而不是collection\u select。为此,我举了一个例子来说明我是如何做到这一点的

<%= f.select(:user, Department.where(:id => current_user.department_id).collect {|p| [[p.title,' (', p.abbreviation,')'], p.id] } + ['Add New Department']) %>
current_user.department_id).收集{p|[[p.title',(',p.缩写')',p.id]}+['addnewdepartment']]]>
然后,当“添加新部门”被选中时,或者按照您计划的方式使用它时,您可以使用javascript之类的东西来执行某些操作


希望这有助于您愉快地编码。

您最好的选择是使用select而不是collection\u select。对于这一点,下面是一个我将如何做到的示例

<%= f.select(:user, Department.where(:id => current_user.department_id).collect {|p| [[p.title,' (', p.abbreviation,')'], p.id] } + ['Add New Department']) %>
current_user.department_id).收集{p|[[p.title',(',p.缩写')',p.id]}+['addnewdepartment']]]>
然后,当“添加新部门”被选中时,或者按照您计划的方式使用它时,您可以使用javascript之类的东西来执行某些操作

希望这有助于和快乐的编码