0

我的 PostgreSQL 数据库中有两个名为users和的表businesses。当用户登录并访问时,business/profile我首先要检查他们是否实际创建了配置文件。如果没有,我想将它们重定向到business/create. 所以基本上,我需要做的是检查businesses表是否包含与当前用户关联的记录。我尝试了一些我认为可行的方法,但不断收到以下错误:

Couldn't find User/Business without an id. 

目前我的路线文件包含以下定义:

get "business/profile"
get "business/account"
get "business/create"

devise_for :users

get "home/index"
root :to => "home#index"
4

2 回答 2

2

在用户和业务模型之间建立关系

class User < ActiveRecord::Base
    has_one :business
  end
class Business < ActiveRecord::Base
    belongs_to :user
end

在 businees_controller.rb 中

class BusinessController < ApplicationController
 def profile
  redirect_to business_create_path unless current_user.business
  "your code"
 end
 def create
 end
end
于 2013-04-28T12:33:29.730 回答
0

我认为你正在使用Business.find_by_...()方法,但如果它没有找到任何东西,它会给你 404 错误。

您应该以这种方式检查它:

def profile
  if current_user.businesses.count == 0
    redirect_to business_create_path
  else
    # ...
  end
end

我想你的模型中有一个 User::belongs_to(Business) 关系。

于 2013-04-28T12:34:21.187 回答