2012-08-03 53 views
0

需要創建用戶後,用戶錢包註冊軌3.1 after_create用戶創建錢包

有用戶模型:

has_one :wallet, :dependent => :destroy 
    after_create :create_wallet 

    def create_wallet 
    self.wallet.create(params[:wallet]) 
    end 

和錢包模型

class Wallet < ActiveRecord::Base 
    belongs_to :user 
end 

時註冊,我得到這個錯誤返回

Started POST "/users" for 127.0.0.1 at 2012-08-03 13:40:41 +0300 
Creating scope :page. Overwriting existing method User.page. 
    Processing by UsersController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"7rgoJQwWFFbo4Wv6gHF2AzwQpCQwB+Pp/kaNRw/YW/k=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign Up"} 
Creating scope :page. Overwriting existing method Product.page. 
    (0.2ms) BEGIN 
    (0.3ms) SELECT 1 FROM `users` WHERE `users`.`email` = BINARY '[email protected]' LIMIT 1 
    (0.3ms) SELECT 1 FROM `users` WHERE `users`.`auth_token` = 'S5R9LGhWVn6ut6Uh4h7KCA==' LIMIT 1 
    SQL (0.2ms) INSERT INTO `users` (`auth_token`, `created_at`, `email`, `password_digest`, `password_reset_sent_at`, `password_reset_token`, `updated_at`) VALUES ('S5R9LGhWVn6ut6Uh4h7KCA==', '2012-08-03 10:40:41', '[email protected]', '$2a$10$KnwA.xrFY4z6eao7SmjHM.dq4ypmZZv3Rnk5LT9pg1jpiwxbl2YkK', NULL, NULL, '2012-08-03 10:40:41') 
Creating scope :page. Overwriting existing method Wallet.page. 
    Wallet Load (0.1ms) SELECT `wallets`.* FROM `wallets` WHERE `wallets`.`user_id` = 4 LIMIT 1 
    (27.2ms) ROLLBACK 
Completed 500 Internal Server Error in 158ms 

NameError (undefined local variable or method `params' for #<User:0x9af5894>): 
    app/models/user.rb:39:in `create_wallet' 
    app/controllers/users_controller.rb:57:in `block in create' 
    app/controllers/users_controller.rb:56:in `create' 
    app/controllers/application_controller.rb:78:in `catch_not_found' 
    app/middleware/flash_session_cookie_middleware.rb:17:in `call' 

Rendered vendor/bundle/ruby/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.8ms) 
Rendered vendor/bundle/ruby/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.7ms) 
Rendered vendor/bundle/ruby/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (3.1ms) 

我建立的代碼是基於這個答案

Rails 3: After_save create profile

我該怎麼辦錯了。請幫忙。謝謝。

回答

3

錯誤undefined local variable or method 'params'清楚地表明params在您的模型中不可用。

此外,在模型中使用參數並不是一個好主意。看到這個問題Rails How to pass params from controller to after_save inside model

此外,即使您在您的模型中有paramsparams[:wallet]也將不可用,因爲它未從Web表單提交。

# No 'wallet' key here. 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7rgoJQwWFFbo4Wv6gHF2AzwQpCQwB+Pp/kaNRw/YW/k=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign Up"} 

UPDATE

創建的錢包不params

has_one :wallet, :dependent => :destroy 

    after_create do |user| 
    user.create_wallet 
    end 
+0

好吧,我得到了它,任何其他方式如何創建用戶時可以創建錢包嗎? – rmagnum2002 2012-08-03 11:22:30

+0

如果您的錢包模型沒有任何需要用戶輸入的字段,您可以使用'self.wallet.create'立即創建它,但是如果您需要用戶輸入,則可以查看嵌套表單(http:///stackoverflow.com/questions/5073698/rails-3-nested-forms)。 – 2012-08-03 11:26:20

+0

錢包有user_id字段,必須在註冊時帶上user.id – rmagnum2002 2012-08-03 11:30:22

0

未定義的局部變量或方法`PARAMS'您的用戶模型