Creating a select box


#1

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


#2

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 %>


#3

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.


DeLynn B.
removed_email_address@domain.invalid


#4

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


#5

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:
http://wiki.rubyonrails.com/rails/pages/Plugins. The second is the
Plugin Directory over at Agile Web D.:
http://agilewebdevelopment.com/plugins.


DeLynn B.
removed_email_address@domain.invalid