2012-03-21 18 views
3

を作成していない私は、ユーザーアカウントにhas_oneの協会およびネストされた属性を持つモデルを持っています。
私の問題は、Devise経由でユーザーにサインアップするときにアカウントが作成されず、ログにエラーが生成されないということです。 私はDevails 2.0.4でRails 3.2を使用しています。工夫登録は協会

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は問題で指摘しました。

こと注:自動保存オプションがaccepts_nested_attributes_for関係団体のために有効にする必要がありますが、手動アップデートの関係を保存したくない場合に使用されます。