Can''t seem to find an example of this anywhere - how do I populate a select list in a form from a database? I have a form to create a new task. The task is for a client, so there should be a select list populated from my clients table. I thought this would work: <%= select ''task'', ''client_id'', Client.find_all, "id", "name" %> but I get this error: undefined method `stringify_keys'' for "name":String Obviously I''m missing something... And then is this going to work properly when I''m using the same form partial to edit the task? thx -- Posted via http://www.ruby-forum.com/.
Here is what I am using:
<p><label for="search_city">City</label><br/>
<%= select("search", "city",
Provider.find_all.collect {|p| [
p.city, p.id ] }, { :include_blank => true }) %>
</p>
It populates the select with the city names but returns the city_id.
Nick Coyne wrote:> Can''t seem to find an example of this anywhere - how do I populate
a
> select list in a form from a database?
>
> I have a form to create a new task. The task is for a client, so there
> should be a select list populated from my clients table. I thought this
> would work:
> <%= select ''task'', ''client_id'',
Client.find_all, "id", "name" %>
>
> but I get this error:
> undefined method `stringify_keys'' for "name":String
>
> Obviously I''m missing something...
>
> And then is this going to work properly when I''m using the same
form
> partial to edit the task?
>
> thx
--
Posted via http://www.ruby-forum.com/.
Kris wrote:> Here is what I am using: > > <p><label for="search_city">City</label><br/> > <%= select("search", "city", Provider.find_all.collect {|p| [ > p.city, p.id ] }, { :include_blank => true }) %> > </p> > > It populates the select with the city names but returns the city_id.Thanks. That works. Also found the form_helpers section in the Agile Rails book, on p357. So I''ve ended up with: <% @clients = Client.find(:all, :order => "name").map {|c| [c.name, c.id] } %> <%= select ''task'', ''client_id'', @clients %> -- Posted via http://www.ruby-forum.com/.
Hi!
in the same problem with a select with some values from db:
the following select in test.rhtml:
<%= select(:listSetting, :code, @listSettings) %>
where
@listSettings = ListSetting.find(:all, :order => "code").map {|u|
[u.code, u.id] }
i have an observer_field defined in test.rhml as below:
<%= observe_field("listSetting_code",
:update => "values_span",
:url => {:action => :listSetting_code}
)
%>
which will observe the select object and run the code from
ListSetting_code method from controller.
the log after changing the select is as below:
Processing ListAdminController#listSetting_code (for 127.0.0.1 at
2006-03-22 14:39:17) [POST]
Parameters: {"action"=>"listSetting_code",
"value"=>"listSetting_code",
"1"=>"",
"controller"=>"list_admin"}
2 short n00b questions:
1. what it means "1"=>"" in the log file?
2. how do I get the value of option selected in controller method?
TIA.
Dan
Nick Coyne wrote:> Kris wrote:
>
>> Here is what I am using:
>>
>> <p><label
for="search_city">City</label><br/>
>> <%= select("search", "city",
Provider.find_all.collect {|p| [
>> p.city, p.id ] }, { :include_blank => true }) %>
>> </p>
>>
>> It populates the select with the city names but returns the city_id.
>>
>
> Thanks. That works. Also found the form_helpers section in the Agile
> Rails book, on p357. So I''ve ended up with:
>
> <% @clients = Client.find(:all, :order => "name").map {|c|
[c.name,
> c.id] } %>
> <%= select ''task'', ''client_id'',
@clients %>
>
>
On 3/22/06, Nick Coyne <nic@nickcoyne.com> wrote:> Kris wrote: > > Here is what I am using: > > > > <p><label for="search_city">City</label><br/> > > <%= select("search", "city", Provider.find_all.collect {|p| [ > > p.city, p.id ] }, { :include_blank => true }) %> > > </p> > > > > It populates the select with the city names but returns the city_id. > > Thanks. That works. Also found the form_helpers section in the Agile > Rails book, on p357. So I''ve ended up with: > > <% @clients = Client.find(:all, :order => "name").map {|c| [c.name, > c.id] } %> > <%= select ''task'', ''client_id'', @clients %>Taking it one small step further, I like to put this code into helpers. Helper code: def all_clients Client.find(:all, :order => "name").map {|c| [c.name, c.id] } end View: <%= select ''task'', ''client_id'', all_clients %> -- James