Reputation: 11
Below is code extract from my file /home/divya/climb/project1/app/views/cities/new.html.erb where line #5 raised this error:
undefined method `map' for nil:NilClass
Extracted source (around line #5):
2: <%= form_for(@city) do |f| %>
3: <%= f.label :country_id %><br />
4:
5: <%= collection_select(:city, :country_id, @countries, :id, :country_name, {:prompt => false}) %>
6: <%= render 'form' %>
7:
8: <%= link_to 'Back', cities_path %>
Rails.root: /home/divya/climb/project
Upvotes: 1
Views: 4812
Reputation: 1
only change this variable in your controller use a global variable $countries=Country.all and your view use this variable.
Upvotes: 0
Reputation: 51151
Apparently you didn't set your @countries
instance variable in controller, so it is nil
.
map
method is called on @countries
internally by ActionView
(to be strict, by options_from_collection_for_select
method).
You should set @countries
in controller, with:
@countries = Country.all
or call it directly in view:
<%= collection_select(:city, :country_id, Country.all, :id, :country_name, { :prompt => false }) %>
Upvotes: 7
Reputation: 47482
Change
<%= collection_select(:city, :country_id, @countries, :id, :country_name, {:prompt => false}) %>
To
<%= collection_select(:city, :country_id, Country.all, :id, :country_name, {:prompt => false}) %>
Upvotes: 2