I encountered some problem when trying to save data into database. The error keep pointing to the CREATE action @invoices = @permits.build\_invoice(). Stating undefined method `build\_invoice' for nil:NilClass NO METHOD ERROR. Anyone know how to fix it?

```
class InvoicesController < ApplicationController

  def new
    @permits = Permit.find(params[:permit_id])
    @invoice = Invoice.new
  end

  def create
    #@current_user = User.find_by(id: session[:user_id])

    @invoices = @permits.build_invoice(invoice_params)
    if @invoices.save
      redirect_to payment_path
    else

    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_invoice
    @invoice = Invoice.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def invoice_params
    params.require(:invoice).permit(:vehicle_type, :name, :department, :carplate, :duration, :permitstart, :permitend, :price, :time)
  end
end

```

Permit.rb

```
class Permit < ApplicationRecord
  belongs_to :user
  has_one :invoice
end

```

Invoice.rb

```
class Invoice < ApplicationRecord
  belongs_to :user
  has_one :receipt
  belongs_to :permit
end

```