Rails 5通过帐户表单创建所有者(嵌套属性)

时间:2018-04-07 21:40:57

标签: ruby-on-rails devise

使用Ruby 2.4.0构建Rails 5应用程序

我有一个用户(设计)模型和一个帐户模型

我正在尝试通过帐户模型创建用户,但是我的日志输出非常不清楚,并且未创建帐户和用户。

日志输出:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__
    return self.func(*args)
  File "C:/Python27/8.py", line 69, in gameOn
    answerIn.delete(0,END)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2509, in delete
    self.tk.call(self._w, 'delete', first, last)
TclError: invalid command name ".94412392"

我的Started POST "/accounts" for 127.0.0.1 at 2018-04-07 15:25:10 -0600 Processing by AccountsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"c7H8MN2pbANsOsJH5PtX93+fAyqoLrBgZ0DfH7wQ8fCsMT0tgbr7TqeBML2/DbW7b4rwEiSlVY9aEFgRsK5uNA==", "owner"=>{"f_name"=>"xxxxxx", "m_name"=>"xxxxxx", "l_name"=>"xxxxxx", "office_country_code"=>"1", "office_telephone_number"=>"4036155915", "country_code"=>"1", "phone_number"=>"4036155915", "email"=>"xxxxxx@xxxxxx.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "account"=>{"subdomain"=>"TestDomain"}, "commit"=>"Create Account"} (1.0ms) BEGIN Account Exists (1.7ms) SELECT 1 AS one FROM "accounts" WHERE LOWER("accounts"."subdomain") = LOWER($1) LIMIT $2 [["subdomain", "testdomain"], ["LIMIT", 1]] (1.1ms) ROLLBACK Redirected to http://localhost:3000/accounts/new Completed 302 Found in 119ms (ActiveRecord: 6.3ms) Started GET "/accounts/new" for 127.0.0.1 at 2018-04-07 15:25:10 -0600 Processing by AccountsController#new as HTML Rendering accounts/new.html.erb within layouts/application Rendered accounts/new.html.erb within layouts/application (114.5ms) Completed 200 OK in 1713ms (Views: 1669.1ms | ActiveRecord: 0.0ms) 型号:

User

我的class User < ApplicationRecord # :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable validates :f_name, :l_name, :country_code, :phone_number, presence: true end 型号:

Account

我的class Account < ApplicationRecord after_initialize :set_default_status, :if => :new_record? before_validation :downcase_subdomain RESTRICTED_SUBDOMAINS = %w(www admin) enum account_status: [:active, :inactive, :suspended, :vip] belongs_to :owner, class_name: 'User' has_one :subscription validates :owner, presence: true validates :subdomain, presence: true, uniqueness: {case_sensitive: false }, format: { with: /\A[\w\-]+\Z/i, message: 'contains invalid caracters'}, exclusion: {in: RESTRICTED_SUBDOMAINS, message: 'this name is restricted'} accepts_nested_attributes_for :owner private def downcase_subdomain self.subdomain = subdomain.try(:downcase) end def set_default_status self.account_status ||= 1 end end 控制器:

Accounts

我的class AccountsController < ApplicationController def new @account = Account.new @account.build_owner end def create @account = Account.new(account_params) if @account.save redirect_to root_path, notice: 'Welcome to RoadRouge - Account created successfully!' else redirect_to new_account_path, alert: 'A problem has occurred. Please try again.' end end private def account_params params.require(:account).permit(:subdomain, :owner_id, :subscription_id, :account_status, owner_attributes: [:f_name, :m_name, :l_name, :office_country_code, :office_telephone_number, :country_code, :phone_number, :email, :password, :password_confirmation]) end end 控制器

Application

我不确定我是否会忽视这里的小事,但我绝对不知所措。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

鉴于查询,看起来它在子域验证时停止了。子域名是否已经存在?尝试显示@account.errors以查看导致问题的原因

答案 1 :(得分:0)

第一个。 :subdomain, uniqueness: {case_sensitive: false }使帐户表的属性subdomain唯一。看起来testdomain可能已经存在。

此外,您确实需要为id添加owner_attributes

def account_params
  params.require(:account).permit(
    :subdomain,
    :subscription_id,
    :account_status,
    owner_attributes: %i[id f_name m_name l_name office_country_code office_telephone_number country_code phone_number email password password_confirmation]
end