Justin Doshay
Justin Doshay

Reputation: 109

Ruby: undefined method `<<' for nil:NilClass on model

I have been stuck at this for a few days now. Any help would be greatly appreciated. i keep getting this nomethoderror for the following code:

band.rb (error is apparently @booked_gigs << set.gig)

  def booked_gigs
    @booked_gig = []
    hired_gigs = self.bandlists.filled
    hired_gigs.each do |set|
     if set.gig.date >= Date.today
      @booked_gigs << set.gig
     end
    end
  end

_booked.html.erb:

<% @booked.each do |booked| %>
  <%= render "shared/gig_full", gig: booked %>
<% end %>

dashboard_controller.rb:

class DashboardController < ApplicationController
  before_filter :authenticate_user!, only: [:dashboard]

  def dashboard
    if current_user.role == "venue"
      set_venue_dash
      render "venue_dashboard"
    elsif current_user.role == "musician"
      set_band_dash
      if @band.nil?
        redirect_to new_band_path
      else
        set_band_session
        set_booked_gig
        set_applied_gig
        render "band_dashboard"
      end
    else
      set_fan_dash
      render "fan_dashboard"
    end
  end

  def settings
  end

  private
    def set_venue_dash
      @venue = current_user.venues.first
    end

    def  set_band_dash
      @band = current_user.bands.first
    end

    def set_band_session
      session[:current_band_id] ||= @band.id
      @openings = @band.available_gigs
    end

    def set_booked_gig
      @booked = current_band.booked_gigs
    end

    def set_applied_gig
      @applied = current_band.applied_gigs
    end

    def set_fan_dash
      @fan = current_user.fan
    end
end

Upvotes: 0

Views: 522

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

Check your variable names. You initialize @booked_gig = [] (singular), but then you try to add items to @booked_gigs (plural).

Also, you probably want to return @booked_gigs at the end of the method, otherwise it will return the contents of hired_gigs (regardless of what's inside your each block)

def booked_gigs
  @booked_gigs = []
  hired_gigs = self.bandlists.filled
  hired_gigs.each do |set|
    if set.gig.date >= Date.today
      @booked_gigs << set.gig
    end
  end
  @booked_gigs
end

Upvotes: 3

Related Questions