Reputation:
I have two tables in my PostgreSQL database named users
and businesses
. When a user logs in and accesses the business/profile
I first want to check if they have actually created a profile. If not, I would then like to redirect them to the business/create
. So basically, what I need to do is check whether the businesses
table contains a record associated with the current user. I've tried a few things I thought would work but keep getting the following error:
Couldn't find User/Business without an id.
Currently my routes file contains the following definitions:
get "business/profile"
get "business/account"
get "business/create"
devise_for :users
get "home/index"
root :to => "home#index"
Upvotes: 0
Views: 2192
Reputation: 8721
I think that you're using Business.find_by_...()
method, but it gives you 404 error if it does not find anything.
You should check it in this way:
def profile
if current_user.businesses.count == 0
redirect_to business_create_path
else
# ...
end
end
I suppose that you have a User::belongs_to(Business) relation in your models.
Upvotes: 0
Reputation: 583
Have a relationship between User and Business model
class User < ActiveRecord::Base
has_one :business
end
class Business < ActiveRecord::Base
belongs_to :user
end
in businees_controller.rb
class BusinessController < ApplicationController
def profile
redirect_to business_create_path unless current_user.business
"your code"
end
def create
end
end
Upvotes: 2