Ask A Question

Notifications

You’re not receiving notifications from this thread.

undefined method `map' for nil:NilClass on select_collection

John Munyi asked in General

Hi,

I have been struggling with this error for the last 3 hours and its s bit frustrating: Here is the scenario

I am trying to populate a select box with Team names that belong to a specific team leader here are my models and code

user.rb
belongs_to :team
  has_many  :attendances, dependent: :destroy
  belongs_to :team_leader, class_name: "User"
  has_many :agents, class_name: "User",
                    foreign_key: "team_leader_id"

team.rb
class Team < ActiveRecord::Base

  validates :team_code, :description, presence: true
  validates :team_code, uniqueness: true

  has_and_belongs_to_many :users
  has_many :attendance_sheets
end


attendance_sheet/new

<div class="field">
            <%= f.label :Team %><br>
            <%= f.collection_select(:team_id, @team, :id, :team_code) %>
          </div>

the other partial with the user, _attendance_fields.html.erb

<div class="nested-fields">
    <tr>
       <td><%= f.collection_select(:user_id, @users, :id, :name ) %></td>
       <td><%= f.check_box :present%></td>
       <td><%= f.select :reason, ["a","b"] %></td>
       <td><%= link_to_remove_association "Remove attendance", f, class: "form-button btn btn-default" %></td>
    </tr>
</div>

whenever i try to create an attendance sheet i the undefined method map error for both the partial and main form. I want to be able to see on teams that belong the user logged and also only the users belonging to the team/logged team_leader

I am not sure if my self join table is well done either

appreciated

Reply

Your first collection_select for @team, is that variable an array? Only arrays (or enumerable objects) have a map method. The map method is called internally inside the collection_select, so if that happens to be just a team record, then that could be the problem.

You'll either want to make that variable an array or adjust that accordingly to how you want it to work I think.

Reply

Hi Chris,

the first part @team is an array the Teams assigned to a specific Team leader

Reply

Hmm, well then it isn't as easy of a solution. ;) Which line of the view is it tripping on then? What's your stacktrace look like?

Reply
Started GET "/attendance_sheets/new" for 127.0.0.1 at 2015-12-11 18:03:36 +0300
Processing by AttendanceSheetsController#new as HTML
  Rendered attendance_sheets/_form.html.erb (3.3ms)
  Rendered attendance_sheets/new.html.erb within layouts/application (4.0ms)
Completed 500 Internal Server Error in 25ms (ActiveRecord: 0.0ms)

ActionView::Template::Error (undefined method `map' for nil:NilClass):
    16:           <% end %>
    17:           <div class="field">
    18:             <%= f.label :Team %><br>
    19:             <%= f.collection_select(:team_id, @team, :id, :team_code) %>
    20:           </div>
    21:           <div class="field">
    22:             <%= f.label :date %><br>
  app/views/attendance_sheets/_form.html.erb:19:in `block in _app_views_attendance_sheets__form_html_erb___3904306276885524695_69942133851520'
  app/views/attendance_sheets/_form.html.erb:5:in `_app_views_attendance_sheets__form_html_erb___3904306276885524695_69942133851520'
  app/views/attendance_sheets/new.html.erb:7:in `_app_views_attendance_sheets_new_html_erb__3506630663733362946_69942134392600'

will that help ?

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.