rmagnum2002
rmagnum2002

Reputation: 11421

rails 3.1 after_create user create wallet

need to create a wallet for user after user sign up

have user model:

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

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

and wallet model

 class Wallet < ActiveRecord::Base
  belongs_to :user
 end

when sign up, I got this error back

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)

the code I built up is based on this answer

Rails 3: After_save create profile

what do I do wrong. Please help. Thank you.

Upvotes: 0

Views: 1248

Answers (2)

Kulbir Saini
Kulbir Saini

Reputation: 3915

The error undefined local variable or method 'params' clearly indicates that params are not available in your model.

Also, it's not a good idea to use params in your models. See this question Rails How to pass params from controller to after_save inside model.

Further, even if you got params in your model, params[:wallet] will not be available because that wasn't submitted from the web form.

# 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

Creating wallet without params

  has_one :wallet, :dependent => :destroy

  after_create do |user|
    user.create_wallet
  end

Upvotes: 4

Abram
Abram

Reputation: 41884

Undefined local variable or method `params' for your user model

Upvotes: 0

Related Questions