Ruby:未定义的方法`<<<'对于nil:模型上的NilClass

时间:2015-06-18 22:37:58

标签: ruby-on-rails ruby ruby-on-rails-3

我已经被困在这几天了。任何帮助将不胜感激。我继续为以下代码获取此nomethoderror:

band.rb(错误显然是@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

1 个答案:

答案 0 :(得分:3)

检查您的变量名称。您初始化@booked_gig = [](单数),但之后您尝试将项目添加到@booked_gigs(复数)。

此外,您可能希望在方法结束时返回@booked_gigs,否则它将返回hired_gigs的内容(无论each块内的内容是什么)

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