2012-03-21 27 views
3

我有一個用戶帳戶機型HAS_ONE協會和嵌套屬性。
我的問題是,通過設計註冊用戶時,帳戶不會創建,並且在日誌中不會生成錯誤。 我正在使用Rails 3.2與Devise 2.0.4。嵌套屬性色器件的註冊也沒有創造協會

user.rb

class User 
    include Mongoid::Document 

    has_one :account, :inverse_of => :user 

    accepts_nested_attributes_for :account 

    field :name 
    validates_presence_of :name 
    validates_uniqueness_of :name, :email, :case_sensitive => false 
    attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :account_attributes 

    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 
    ... 
end 

account.rb

class Account 
    include Mongoid::Document 

    belongs_to :user, :inverse_of => :account 

    field :name 

    validates_presence_of :user 
    attr_accessible :name, :user_id 

end 

registration_controller.rb

class RegistrationsController < Devise::RegistrationsController 
    def new 
    resource = build_resource({}) 
    resource.build_account 
    respond_with resource 
    end 
end 

視圖

<h2>Sign up</h2> 

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> 
    <%= devise_error_messages! %> 

    <%= f.fields_for :account do |account_form| %> 
    <div><%= account_form.label 'Company' %> 
    <%= account_form.text_field :name %></div> 
    <% end %> 
... 

這裏是我的控制檯輸出

Started POST "/users" for 127.0.0.1 at 2012-03-21 03:40:55 -0700 
Processing by RegistrationsController#create as HTML 
    Parameters: {"utf8"=>"✓",   "authenticity_token"=>"dAmJrthe/IjlHzjBI2kF9nkTxwIWM0o69Q1PI2nd95o=", "user"=>{"account_attributes"=>{"name"=>"comp11"}, "name"=>"user11", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} 
MONGODB (1ms) myapp_development['$cmd'].find({"count"=>"users", "query"=>{:email=>"[email protected]"}, "fields"=>nil}).limit(-1) 
MONGODB (0ms) myapp_development['$cmd'].find({"count"=>"users", "query"=>{:name=>/^user11$/i}, "fields"=>nil}).limit(-1) 
MONGODB (0ms) myapp_development['$cmd'].find({"count"=>"users", "query"=>{:email=>/^[email protected]\.com$/i}, "fields"=>nil}).limit(-1) 
MONGODB (1ms) myapp_development['users'].insert([{"email"=>"[email protected]", "encrypted_password"=>"$2a$10$lY6aHKTyeVALAcIkX.Ipke5YMj7/viU9Hy5s.jsQAq7cfCBtJtXaO", "sign_in_count"=>0, "_id"=>BSON::ObjectId('4f69b0377e3d3639bf000008'), "name"=>"user11"}]) 
MONGODB (0ms) myapp_development['users'].update({"_id"=>BSON::ObjectId('4f69b0377e3d3639bf000008')}, {"$set"=>{"last_sign_in_at"=>2012-03-21 10:40:56 UTC, "current_sign_in_at"=>2012-03-21 10:40:56 UTC, "last_sign_in_ip"=>"127.0.0.1", "current_sign_in_ip"=>"127.0.0.1", "sign_in_count"=>1}}) 
Redirected to http://localhost:3000/ 
Completed 302 Found in 257ms 

正如你所看到的,我沒有看到有關創建帳戶什麼。還驗證沒有帳戶集合是通過查看MongoDB創建的。我錯過了什麼? 感謝您的幫助提前。

回答

2

解決了這個問題。
我需要打開:自動保存

has_one :account, :inverse_of => :user, :autosave => true 

mongoid docs指出這個問題。

請注意:如果您不想在更新時手動保存關係,則需要爲關係關聯啓用自動保存選項,該關聯關聯用於accept_nested_attributes_for。