1

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

3 Answers 3

7

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 }) %>
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

only change this variable in your controller use a global variable $countries=Country.all and your view use this variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.