Drop down menu

I have a series of countries listed in a table in the following form:

id, country_name

I want to pull these in and I’m trying everything under the sun from a
variety of different peoples pages. I just cannot get anything working
at all. I have tried pulling all the records in the the ctrl.rb file
with:

@country_list = Country.find_all

I get the following:

undefined method `find_all’ for #Class:0xb657a3a8

I have put this in the following method

def new
@usr = Usr.new
@country_list = Country.find_all
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @usr }
end
end

I’m not fussy about the methodology, I simply want a drop down list the
easiest and simplest way…I have tried a number of other approaching,
but I wonder if there are problems with RoR versions?

Thanks in advance.

disruptive tech wrote:

I have a series of countries listed in a table in the following form:

id, country_name

I want to pull these in and I’m trying everything under the sun from a
variety of different peoples pages. I just cannot get anything working
at all. I have tried pulling all the records in the the ctrl.rb file
with:

@country_list = Country.find_all

I get the following:

undefined method `find_all’ for #Class:0xb657a3a8

I have put this in the following method

def new
@usr = Usr.new
@country_list = Country.find_all
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @usr }
end
end

I’m not fussy about the methodology, I simply want a drop down list the
easiest and simplest way…I have tried a number of other approaching,
but I wonder if there are problems with RoR versions?

Thanks in advance.

I also tried directly in the view:

collection_select ‘country’, ‘type’, Type.find(:all), :type_name, :id

with nothing in the controller.

I’m also trying this:

controller

def new
@usr = Usr.new
@country_list = Country.find(:all, :order=>“country_name”)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @usr }
end
end

and in view

<%= collection_select(:country, :id, @country_list, :id, :country_name,
options ={:prompt => “-Select a cnt”}, :class =>“country”) %>

I get the following on loading a new.html.erb

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map

Any ideas? Thanks

B

Colin L. wrote:

On 4 August 2010 09:32, disruptive tech [email protected] wrote:

� � �format.xml �{ render :xml => @usr }

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map

What happens if, rather than using collection select, you just display
the id and country name for each item in @country list? Doing that
will prove that the data is ok. I presume that the table has a
country_name column.

Colin

here is a read-out - the data is there…

Listing countries

Country name
UK Show Edit Destroy
France Show Edit Destroy
Germany Show Edit Destroy

describe countries;
±-------------±-------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±-------------±-------------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| country_name | varchar(255) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
±-------------±-------------±-----±----±--------±---------------+
4 rows in set (0.00 sec)

select * from countries;
±—±-------------±--------------------±--------------------+
| id | country_name | created_at | updated_at |
±—±-------------±--------------------±--------------------+
| 4 | UK | 2010-08-02 16:37:43 | 2010-08-02 16:37:43 |
| 5 | France | 2010-08-02 16:37:51 | 2010-08-02 16:37:51 |
| 6 | Germany | 2010-08-04 08:20:03 | 2010-08-04 08:20:03 |
±—±-------------±--------------------±--------------------+
3 rows in set (0.00 sec)

On 4 August 2010 09:32, disruptive tech [email protected] wrote:

 format.xml  { render :xml => @usr }

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map

What happens if, rather than using collection select, you just display
the id and country name for each item in @country list? Doing that
will prove that the data is ok. I presume that the table has a
country_name column.

Colin

On 4 August 2010 10:56, disruptive tech [email protected] wrote:

will prove that the data is ok. Â I presume that the table has a
France  Show  Edit  Destroy
Germany  Show  Edit  Destroy

Is that from code inserted in the view at the same point as the
collection_select?

Please show the complete error trace and the code around the error,
and confirm which line of code the error is on.

Colin

I can see data in the DB, but cannot seem to get this into a drop down.
The controller code does not produce an error. But the errors would seem
to imply that there is no data being pulled in.

Extracted source (around line #23):

20: <%= f.text_field :email %>
21:


22:


23: <%= collection_select(:country, :id, @country_list, :id,
:country_name,
24: options ={:prompt => “-Select a cnt”}, :class =>“country”) %>
25: <%= f.label :country %>

26: <%= f.text_field :country %>
RAILS_ROOT: /home/brett/RoR/Apps/OpenBenefit/benefit

Application Trace | Framework Trace | Full Trace
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/form_options_helper.rb:327:in
options_from_collection_for_select' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/form_options_helper.rb:543:in to_collection_select_tag’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/form_options_helper.rb:162:in
collection_select' /home/brett/RoR/Apps/OpenBenefit/benefit/app/views/usrs/new.html.erb:23:in _run_erb_app47views47usrs47new46html46erb’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/form_helper.rb:499:in
fields_for' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/form_helper.rb:282:in form_for’
/home/brett/RoR/Apps/OpenBenefit/benefit/app/views/usrs/new.html.erb:3:in
_run_erb_app47views47usrs47new46html46erb' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/renderable.rb:34:in send’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/renderable.rb:34:in
render' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/base.rb:306:in with_template’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/renderable.rb:30:in
render' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/template.rb:205:in render_template’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/base.rb:265:in
render' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/base.rb:348:in _render_with_layout’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/base.rb:262:in
render' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:1250:in render_for_file’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:942:in
render_without_benchmark' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:51:in render’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in
ms' /usr/lib/ruby/1.8/benchmark.rb:308:in realtime’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in
ms' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:51:in render’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:135:in
send' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:135 /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:179:in call’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:179:in
respond' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:173:in each’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:173:in
respond' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:107:in respond_to’
/home/brett/RoR/Apps/OpenBenefit/benefit/app/controllers/usrs_controller.rb:38:in
new' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in send’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in
perform_action_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:617:in call_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:610:in
perform_action_without_benchmark' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in
ms' /usr/lib/ruby/1.8/benchmark.rb:308:in realtime’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in
ms' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/rescue.rb:160:in
perform_action_without_flash' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/flash.rb:151:in perform_action’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in
send' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in process_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/filters.rb:606:in
process' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:391:in process’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:386:in
call' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/routing/route_set.rb:438:in call’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:87:in
dispatch' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:121:in _call’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:130
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/query_cache.rb:29:in
call' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/query_cache.rb:29:in call’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in
cache' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/query_cache.rb:9:in cache’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/query_cache.rb:28:in
call' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in call’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/string_coercion.rb:25:in
call' /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/head.rb:9:in call’
/usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/methodoverride.rb:24:in
call' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/params_parser.rb:15:in call’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/session/cookie_store.rb:99:in
call' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/failsafe.rb:26:in call’
/usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in call' /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in synchronize’
/usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in call' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:114:in call’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/reloader.rb:34:in
run' /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/dispatcher.rb:108:in call’
/usr/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/rack/static.rb:31:in
call' /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:47:in call’
/usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:41:in each' /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:41:in call’
/usr/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/rack/log_tailer.rb:17:in
call' /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/content_length.rb:13:in call’
/usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/handler/webrick.rb:48:in
service' /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in service’
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /usr/lib/ruby/1.8/webrick/server.rb:173:in start_thread’
/usr/lib/ruby/1.8/webrick/server.rb:162:in start' /usr/lib/ruby/1.8/webrick/server.rb:162:in start_thread’
/usr/lib/ruby/1.8/webrick/server.rb:95:in start' /usr/lib/ruby/1.8/webrick/server.rb:92:in each’
/usr/lib/ruby/1.8/webrick/server.rb:92:in start' /usr/lib/ruby/1.8/webrick/server.rb:23:in start’
/usr/lib/ruby/1.8/webrick/server.rb:82:in start' /usr/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/handler/webrick.rb:14:in run’
/usr/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/commands/server.rb:111
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in
gem_original_require' /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in require’
script/server:3

disruptive tech wrote:

Colin L. wrote:

On 4 August 2010 09:32, disruptive tech [email protected] wrote:

� � �format.xml �{ render :xml => @usr }

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map

What happens if, rather than using collection select, you just display
the id and country name for each item in @country list? Doing that
will prove that the data is ok. I presume that the table has a
country_name column.

Colin

here is a read-out - the data is there…

Listing countries

Country name
UK Show Edit Destroy
France Show Edit Destroy
Germany Show Edit Destroy

describe countries;
±-------------±-------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±-------------±-------------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| country_name | varchar(255) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
±-------------±-------------±-----±----±--------±---------------+
4 rows in set (0.00 sec)

select * from countries;
±—±-------------±--------------------±--------------------+
| id | country_name | created_at | updated_at |
±—±-------------±--------------------±--------------------+
| 4 | UK | 2010-08-02 16:37:43 | 2010-08-02 16:37:43 |
| 5 | France | 2010-08-02 16:37:51 | 2010-08-02 16:37:51 |
| 6 | Germany | 2010-08-04 08:20:03 | 2010-08-04 08:20:03 |
±—±-------------±--------------------±--------------------+
3 rows in set (0.00 sec)

On 4 August 2010 11:10, Colin L. [email protected] wrote:

the id and country name for each item in @country list? Â Doing that
UK  Show  Edit  Destroy
France  Show  Edit  Destroy
Germany  Show  Edit  Destroy

Is that from code inserted in the view at the same point as the
collection_select?

You replied showing the trace (but not actually as a reply to this
post) but did not answer the above question

Colin

Colin L. wrote:

On 4 August 2010 11:10, Colin L. [email protected] wrote:

the id and country name for each item in @country list? Â Doing that
UK  Show  Edit  Destroy
France  Show  Edit  Destroy
Germany  Show  Edit  Destroy

Is that from code inserted in the view at the same point as the
collection_select?

You replied showing the trace (but not actually as a reply to this
post) but did not answer the above question

Colin

Problem solved, I had not put the @country_list into the other methods -
such as the index one. This now worked and I can create a drop down
populated from the table.

Now I am presuming the select statement will pop the id into the country
field in my created users table. However I cannot seem to find it in the
actual data tables.

Plus also how best to validate drop down boxes?

On 4 August 2010 14:15, disruptive tech [email protected] wrote:

You replied showing the trace (but not actually as a reply to this
post) but did not answer the above question

Colin

Problem solved, I had not put the @country_list into the other methods -
such as the index one. This now worked and I can create a drop down
populated from the table.

Hmm, so the error in new.html.erb was due to the fact that you had not
populated @country_list in the controller#index. I think not.

Now I am presuming the select statement will pop the id into the country
field in my created users table. However I cannot seem to find it in the
actual data tables.

No idea what you are talking about. All a select does is to allow a
selection.

Plus also how best to validate drop down boxes?

Validation is nothing to do with how the data are selected. It is
purely a verification of the data as it goes into the db via the
model.

Colin