Forum: Ruby on Rails creating a select box

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fcf5e28e2e7ce245632a4a29896e6799?d=identicon&s=25 Guest (Guest)
on 2006-04-24 17:56
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
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-24 18:07
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 %>
81279142caec7e29150fced7f916da62?d=identicon&s=25 DeLynn Berry (Guest)
on 2006-04-24 19:54
(Received via mailing list)
> 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 Berry
delynn@gmail.com
http://delynnberry.com
Fcf5e28e2e7ce245632a4a29896e6799?d=identicon&s=25 Guest (Guest)
on 2006-04-25 11:21
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
81279142caec7e29150fced7f916da62?d=identicon&s=25 DeLynn Berry (Guest)
on 2006-04-25 14:18
(Received via mailing list)
> 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 Development:
http://agilewebdevelopment.com/plugins.

--
DeLynn Berry
delynn@gmail.com
http://delynnberry.com
This topic is locked and can not be replied to.