Reputation: 823
I have a has_one
association in my model with my user. What I'm trying to do here is simple but I'm having a hard time understanding whats wrong here. So since I have a has_one association with my model, in my mind I was simply thinking that if the user has already created the model associated with the has_one
association if he tries accessing "localhost3000/model/new"
I would redirect him to the edit page of this particular model. Here is what I have but its telling me its not working as intended. It's as if my if statement is not catching anything
class BusinessesController < ApplicationController
before_action :set_business, only: [:show, :edit, :update, :destroy]
def index
@businesses = Business.all
@buzzs = Buzz.all
end
def show
end
def new
if current_user.business.nil?
@business = current_user.build_business
else
render 'edit'
end
end
def edit
end
def create
@business = current_user.build_business(business_params)
if @business.save
redirect_to @business, notice: 'Business was successfully created.'
else
render "new"
end
end
This error does not make a lot of sense to me because it says its an error in the "new" controller which would have rendered it to the edit path thus not being nil
Upvotes: 0
Views: 18
Reputation: 2530
This is happening because you're not setting @business
when redirecting to 'edit'. Try this:
def new
if current_user.business.nil?
@business = current_user.build_business
else
@business = current_user.business
render 'edit'
end
end
Upvotes: 2