rails 3.1 after_create用户创建钱包

时间:2012-08-03 10:43:56

标签: ruby-on-rails ruby-on-rails-3

用户注册后需要为用户创建钱包

拥有用户模型:

  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"=>"rmagnum2002@gmail.com", "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 'rmagnum2002@gmail.com' 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', 'rmagnum2002@gmail.com', '$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

我做错了什么。请帮忙。谢谢。

2 个答案:

答案 0 :(得分:3)

错误undefined local variable or method 'params'清楚地表明您的模型中没有params

此外,在模型中使用params并不是一个好主意。请参阅此问题Rails How to pass params from controller to after_save inside model

此外,即使您的模型中有paramsparams[:wallet]也无法使用,因为没有从网络表单提交。

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

<强>更新

创建没有params

的钱包
  has_one :wallet, :dependent => :destroy

  after_create do |user|
    user.create_wallet
  end

答案 1 :(得分:0)

用户模型的未定义局部变量或方法`params'