Hi is it possible to manually add an option to collection_select
I have the following code and want to add “Add Customer” to the bottom
of the options list.
<% @customers = Customer.find(:all, :order => “company”)%>
<%= f.collection_select(:customer_id, @customers,:id, :company) %>
Thanks
Martin
On 5/14/08, Martin E. [email protected] wrote:
Hi is it possible to manually add an option to collection_select
I have the following code and want to add “Add Customer” to the bottom
of the options list.
<% @customers = Customer.find(:all, :order => “company”)%>
@customers is an array, so you want to use the array method ‘+’ to add
your new element to it.
http://www.ruby-doc.org/core/classes/Array.html#M002232
<%= f.collection_select(:customer_id, @customers,:id, :company) %>
–
Greg D.
http://destiney.com/
You probably want to get a little lower-level than collection_select,
which is designed to simplify creating your select tag from a
collection of objects. Since you’re adding an option to that
collection, you should go with either select_tag, or select, adding
options_for_select( Customer.find(:all).map{|c|[c.id,c.company} +
[‘add_a_customer’, ‘Add Customer’])
as the “options” parameter.
Take a look at the API for the usage of “select” or “select_tag”. Also
depends on whether you’re using a form helper.
On May 26, 3:45 pm, Martin E. [email protected]