2016-10-27 63 views
0

我有一個註冊表單,要求提供四件事情:用戶的電子郵件,用戶名,公司名稱和密碼。註冊表中的belongs_to記錄的反記錄不保存屬性

用戶模型包含電子郵件,名稱和密碼字段

class User < ApplicationRecord 
    belongs_to :account 
    attr_accessor :account_attributes 
    accepts_nested_attributes_for :account 


    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

end 

帳戶模型包含了公司的名稱。

class Account < ApplicationRecord 
    has_many :users, 
      :dependent => :destroy 

    attr_accessor :name 

    validates :name, presence: true 
end 

我的登記表的樣子(使用超薄模板):

h2 
    | Sign up 
= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| 
    = devise_error_messages! 
    .field 
    = f.label :email 
    br 
    = f.email_field :email, autofocus: true 
    .field 
    = f.label :name, "Your name" 
    br 
    = f.text_field :name 
    = f.fields_for :account_attributes do |a| 
    .field 
     = a.label :name, "Your company or organization" 
     br 
     = a.text_field :name 
    .field 
    = f.label :password 
    - if @minimum_password_length 
     em 
     | (
     = @minimum_password_length 
     | characters minimum) 
    br 
    = f.password_field :password, autocomplete: "off" 
    .field 
    = f.label :password_confirmation 
    br 
    = f.password_field :password_confirmation, autocomplete: "off" 
    .actions 
    = f.submit "Sign up" 
= render "devise/shared/links" 

最後,我自定義的註冊控制器:

class RegistrationsController < Devise::RegistrationsController 
    def new 
    super 
    resource.build_account 
    end 

    def create 
    super 
    resource.account ||= Account.new 
    resource.account.name = sign_up_params[:account_attributes]["name"] 
    resource.account.save 
    end 

    private 

    def sign_up_params 
    params.require(:user).permit(:first_name, :name, :email, :password, :password_confirmation, :account_attributes => [:name]) 
    end 
end 

帳戶關係本身仍然存在,但戶口NAME不會持續。查看服務器日誌,我可以看到該帳戶的持續位置,但我沒有看到該名稱持續存在。奇怪的是,我可以在服務器中看到它嘗試執行事務,但最終不會發出任何SQL更新語句(並且不會發出指示沒有驗證錯誤的回滾)。

SQL (0.2ms) INSERT INTO "accounts" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2016-10-27 16:39:33 UTC], ["updated_at", 2016-10-27 16:39:33 UTC]] 
    SQL (0.2ms) INSERT INTO "users" ("name", "account_id", "created_at", "updated_at", "email", "encrypted_password") VALUES (?, ?, ?, ?, ?, ?) [["name", "dsfdsfds"], ["account_id", 32], ["created_at", 2016-10-27 16:39:33 UTC], ["updated_at", 2016-10-27 16:39:33 UTC], ["email", "[email protected]"], ["encrypted_password", "$2a$11$pUhS5LGJO2VjPvlPVwj0KO6Ce5Ysr8s4Cu.R4kmsWe7CEayk7t8Fm"]] 
    (2.2ms) commit transaction 
    (0.0ms) begin transaction 
    (0.0ms) commit transaction 
    (0.0ms) begin transaction 
    SQL (0.3ms) UPDATE "users" SET "updated_at" = ?, "sign_in_count" = ?, "current_sign_in_at" = ?, "last_sign_in_at" = ?, "current_sign_in_ip" = ?, "last_sign_in_ip" = ? WHERE "users"."id" = ? [["updated_at", 2016-10-27 16:39:33 UTC], ["sign_in_count", 1], ["current_sign_in_at", 2016-10-27 16:39:33 UTC], ["last_sign_in_at", 2016-10-27 16:39:33 UTC], ["current_sign_in_ip", "::1"], ["last_sign_in_ip", "::1"], ["id", 36]] 
    (0.5ms) commit transaction 
Redirected to http://localhost:3000/products 

我把我的頭髮撕了出來,我研究了我能找到的每個SO問題,看起來都很相關。我可以在控制器中執行puts sign_up_params[:account_attributes]["name"],我輸入表單的名稱可以正確打印,我似乎無法將其分配給模型。

回答

0

賬戶模型上的attr_accessor是什麼引起的。通過定義一個attr_accessor,我重寫了用於數據庫列的屬性訪問器中構建的Rails。

只需從帳戶模型中刪除attr_accessor :name即可解決此問題。