title: NoMethodError in EventsController#create undefined method `build' for nil:NilClass?
body: <p>I'm getting a bit confused as to why I am receiving this error as (i think) it is suggesting that my current_customer returns nil. I do not know how to fix this, is it a problem with my current_customer method, sessions controller, or my events_controller... </p>
  
  <p>Event Model:</p>
  
  <pre><code>class Event &lt; ActiveRecord::Base
    belongs_to :calendar
  
  end
  </code></pre>
  
  <p>Events Controller:</p>
  
  <pre><code>class EventsController &lt; ApplicationController
  
    def new
      @event = Event.new
      @calendar = current_customer.calendar #WHY IS IT NOT ERRORING HERE TOO?
  
    end
  
  
    def create
  
      **@calendar = current_customer.calendar #IT IS CALLING ERROR HERE**
      @event = @calendar.build.event(event_params)
      if @event.save
        redirect_to '/main'
      else
        redirect_to '/compose'
      end
    end
  
  
    private
    def event_params
      params.require(:event).permit(:calendar_id, :name, :starts_at, :ends_at) 
    end
  </code></pre>
  
  <p>Customer model:</p>
  
  <pre><code>class Customer &lt; ActiveRecord::Base
    belongs_to :business
    has_one :calendar
  
    has_secure_password
  
    attr_accessor :remember_token
  
    #remembers a user in the database for  use in persistent sessions
    def remember
      self.remember_token = Customer.new_token
      update_attribute(:remember_digest, Customer.digest(remember_token))
    end
  
    def Customer.digest(string)
      cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
          BCrypt::Engine.cost
      BCrypt::Password.create(string, cost: cost)
    end
  
    def forget
      update_attribute(:remember_digest, nil)
    end
  
    def Customer.new_token
      SecureRandom.urlsafe_base64
    end
  
    #returns true if the given token matches the digest
    def authenticated?(remember_token)
      BCrypt::Password.new(remember_digest).is_password?(remember_token)
    end
  
  
  end
  </code></pre>
  
  <p>Customer controller:</p>
  
  <pre><code>class CustomersController &lt; ApplicationController
  
    def new
  
      @customer = Customer.new
      @businesses = Business.all
      @calendar = Calendar.new
  
    end
  
    def create
  
      @customer = Customer.create(customer_params)
  
      @calendar = @customer.build_calendar
      @customer.save!
      session[:customer_id] = @customer.id
  
      redirect_to '/'
    rescue ActiveRecord::RecordInvalid =&gt; ex
      render action: 'new', alert: ex.message
    end
  
    private
    def customer_params
      params.require(:customer).permit(:first_name, :last_name, :business_no, :email, :password, :business_id)
    end
  
  end
  </code></pre>
  
  <p>Sessions controller:</p>
  
  <pre><code>class SessionsController &lt; ApplicationController
  
    def new
  
    end
  
    def create
      @customer = Customer.find_by_email(params[:session][:email])
      if @customer &amp;&amp; @customer.authenticate(params[:session][:password])
        session[:customer_id] = @customer.id #log in
        @customer.remember
        cookies.permanent.signed[:customer_id] = @customer.id
        cookies.permanent[:remember_token] = @customer.remember_token
        redirect_to '/main'
      else
        #need to add flash error: invalid password/email combination
        redirect_to '/login'
      end
    end
  
    def destroy
      @current_customer.forget
      cookies.delete(:customer_id)
      cookies.delete(:remember_token)
      session.delete(:customer_id)
      @current_customer = nil
      redirect_to '/'
    end
  end
  </code></pre>
  
  <p>Current_customer method within application.controller:</p>
  
  <pre><code>helper_method :current_customer
  
    def current_customer
      if (customer_id = session[:customer_id])
        @current_customer ||= Customer.find_by(id: customer_id)
      elsif (customer_id = cookies.signed[:customer_id])
        customer = Customer.find_by(id: customer_id)
        if customer &amp;&amp; customer.authenticated?(cookies[:remember_token])
          session[:customer_id] = customer.id #log in
          @current_customer = customer
  
        end
      end
    end
  </code></pre>
  
  <p>Calendar model:</p>
  
  <pre><code>class Calendar &lt; ActiveRecord::Base
    belongs_to :customer
    has_many :events
  
  end
  </code></pre>
  
  <p>calendars_controller:</p>
  
  <pre><code>class CalendarsController &lt; ApplicationController
  
    def new
      @calendar = Calendar.new(calendar_params)
  
    end
  
    def create
      @calendar = Calendar.new(calendar_params)
    end
  
  private
    def calendar_params
      params.require(:customer_id)
    end
  
  end
  </code></pre>
  
  <p>I am new to Ruby/Rails so I would really appreciate any explanations and answers, please help! thanks</p>