0

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

有用户模型:

  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 创建配置文件

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

4

2 回答 2

4

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

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

此外,即使您进入params您的模型,params[:wallet]也将不可用,因为它不是从 Web 表单提交的。

# 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
于 2012-08-03T11:18:55.917 回答
0

您的用户模型的未定义局部变量或方法“参数”

于 2012-08-03T11:19:31.380 回答