# Creating a select box

**URL:** <https://www.ruby-forum.com/t/creating-a-select-box/94184>\
**Category:** Rails\
**Created:** [April 24, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/creating-a-select-box/94184 "2006-04-24T17:56:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 24, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/creating-a-select-box/94184/1 "2006-04-24T17:56:33Z")

</div>

Hi

trying to create a select box in \_form.rhtml.

I have a table called organisations that contains fileds, 2 being ‘id’  
and ‘name’

these are the fields I need to bring over to the clients \_form.rhtml.

whats the best way of going about that??

2 ways i have seen suggested that I cant get to work  
-@organisations = Organisation.find\_all placed this in def new in  
clients\_controller.rb

but jsut get an error message when trying to extract the data, not sure  
if it would work if there’s no organisations entered either!

-\<%  
@organisations = Organisation.find(:all, :order =\> “name”)  
collection\_select(:Organisation, :name, @organisations, :id, :name)  
%\>

have also tried putting this in the \_form.rhtml, but nothing seems to  
happen.

any suggestions??

my models look like this

class Organisation \< ActiveRecord::Base  
has\_one :clients  
end

class Client \< ActiveRecord::Base  
belongs\_to :organisation  
end

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 24, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/creating-a-select-box/94184/2 "2006-04-24T18:07:21Z")

</div>

Guest wrote:

> Hi
> 
> trying to create a select box in \_form.rhtml.
> 
> I have a table called organisations that contains fileds, 2 being ‘id’  
> and ‘name’
> 
> these are the fields I need to bring over to the clients \_form.rhtml.
> 
> whats the best way of going about that??
> 
> 2 ways i have seen suggested that I cant get to work  
> -@organisations = Organisation.find\_all placed this in def new in  
> clients\_controller.rb
> 
> but jsut get an error message when trying to extract the data, not sure  
> if it would work if there’s no organisations entered either!
> 
> -\<%  
> @organisations = Organisation.find(:all, :order =\> “name”)  
> collection\_select(:Organisation, :name, @organisations, :id, :name)  
> %\>
> 
> have also tried putting this in the \_form.rhtml, but nothing seems to  
> happen.
> 
> any suggestions??
> 
> my models look like this
> 
> class Organisation \< ActiveRecord::Base  
> has\_one :clients  
> end
> 
> class Client \< ActiveRecord::Base  
> belongs\_to :organisation  
> end

First of all, watch the plural on has\_one. That should say “has\_one  
:client”.

And here is the proper structure for the collection\_select method:

collection\_select(object, method, collection, value\_method, text\_method,  
options = {}, html\_options = {})

The first parameter, object, is the name of the instance variable the  
model object is in that you want to edit/create. So assuming your  
controller has this:

def new  
@client = Client.new  
end

then your view to make a select box should be:

\<% orgs = Organization.find(:all, :order =\> ‘name’) %\>  
\<%= collection\_select ‘client’, ‘organization’, orgs, :name, :id %\>

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 24, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/creating-a-select-box/94184/3 "2006-04-24T19:54:22Z")

</div>

> whats the best way of going about that??

You might be interested in using my acts\_as\_dropdown plugin. It will  
allow you to do something like this:

class Organization \< ActiveRecord::Base  
has\_one :client  
acts\_as\_dropdown :order =\> ‘name’  
end

Then in your view, do:

\<%= select ‘client’, ‘organization\_id’, Organization.to\_dropdown %\>

You can read more about this plugin here:  
[http://delynnberry.com/pages/acts\_as\_dropdown](http://delynnberry.com/pages/acts_as_dropdown).

–  
DeLynn B.  
removed\_email\_address@domain.invalid

> **[DeLynn Berry](https://delynnberry.com)**

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 25, 2006, 11:21am UTC](https://www.ruby-forum.com/t/creating-a-select-box/94184/4 "2006-04-25T11:21:26Z")

</div>

Morning,

Thanks for both ideas, have tried them both out, and they seem to work  
well.

As for plugins, does anyone know of any good sites listing all these  
plugin’s?

Scott

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 25, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/creating-a-select-box/94184/5 "2006-04-25T14:18:26Z")

</div>

> As for plugins, does anyone know of any good sites listing all these  
> plugin’s?

There are two good locations for Plugins. The first is the Rails Wiki:  
[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/Plugins). The second is the  
Plugin Directory over at Agile Web D.:  
[http://agilewebdevelopment.com/plugins](http://agilewebdevelopment.com/plugins).

–  
DeLynn B.  
removed\_email\_address@domain.invalid

> **[DeLynn Berry](https://delynnberry.com)**
