samedi 5 mars 2016

Devise undefined method `build' for nil:NilClass

I am currently enrolled in a online course in ruby on rails and im learning everything from scratch so pardon me if Im not clear in my questions. Here is my delima, I am trying to create a LINK between two Models in the rails application and here is what I have so far. However when I try to access localhost3000/business/new it returns the error mentioned it the title. I have come to the conclusion that it is because of me using a "has_one :model" type of association, rather than the "has_many :model". I hope someone can point me in the right direction here as I have spent hours searching to no solutions.

class BusinessesController < ApplicationController
  before_action :set_business, only: [:show, :edit, :update, :destroy]
  //edited for clarity
  def new
    @business = current_user.business.build  //The line that returns a undefined method for nil class
  end

  def edit
  end

  def create
    @business = current_user.business.build(business_params)

    if @business.save
      redirect_to @business, notice: 'Business was successfully created.' 
    else
      render "new"
    end
  end

Devise User Model

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :buzzs, dependent: :destroy
  has_one :business, dependent: :destroy
end

Business Model

class Business < ActiveRecord::Base
    belongs_to :user
end

Aucun commentaire:

Enregistrer un commentaire