【发布时间】:2019-08-27 21:36:54
【问题描述】:
使用 ruby 2.6 和 rails 5.2 Rails 控制台中的用户创建引发
ActiveRecord::RecordInvalid (Validation failed: Account must exist)
我刚刚生成了设计用户,所以暂时没有user_controller
型号
user.rb
class User < ApplicationRecord
belongs_to :account
end
account.rb
class Account < ApplicationRecord
has_many :users
end
控制器
accounts_controller.rb
class AccountsController < ApplicationContoller
def new
redirect_to root_path
unless current_user.account.nil?
@account = Account.new
end
def create
@account = Account.new(account_params)
if @account.save
current_user.account = @account
current_user.save
redirect_to root_path, success: "Your account has been created!"
else
render :new
end
end
........
end
【问题讨论】:
标签: ruby-on-rails ruby