collection_select

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

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

Greg Donald 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.

class Array - RDoc Documentation

<%= f.collection_select(:customer_id, @customers,:id, :company) %>

-- Greg Donald http://destiney.com/

I can see that it is an array and I've looked at the documentation and have tried a few ways of getting it to work but have failed miserably can you give me an example of how I would add an option manually this is what I have for the select @customers = Customer.find(:all, :select => "id, company") => [#<Customer id: 6, company: "Ernie's Bakery">, #<Customer id: 7, company: "Young's the Bakers">, #<Customer id: 8, company: "Suggs Newsagents">]

thanks Martin

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.