I asked this in the Rails mailing list but nobody answered me, I think
it is a newbie (I am a newbie) error but
I don’t see the light.
In a view I have a link to a controller’s action
(TeamController#add_player) that calls to Player.create_player method,
that initialize a player object then try to save it, but when it calls
to save method an error happens:
NoMethodError in TeamController#add_player
You have a nil object when you didn’t expect it!
The error occured while evaluating nil.keys
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1557:in
attribute_names' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:2060:in
clone_attributes’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1519:in
attributes' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1945:in
attributes_with_quotes’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1725:in
update_without_lock' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/locking.rb:33:in
update_without_callbacks’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/callbacks.rb:274:in
update_without_timestamps' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/timestamp.rb:39:in
update’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1718:in
create_or_update_without_callbacks' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/callbacks.rb:249:in
create_or_update’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1392:in
save_without_validation' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:724:in
save_without_transactions’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:in
save' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in
transaction’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:91:in
transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:118:in
transaction’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:in
save' #{RAILS_ROOT}/app/models/player.rb:15:in
create_player’
app/controllers/team_controller.rb:53:in `add_player’
And my Player model is:
class Player < ActiveRecord::Base
belongs_to :team
Crea un jugador con los valores por defecto
def Player.create_player(team_id)
player = Player.new
player.relate_to_team(team_id)
player.save <-------------------------------- LINE 15
return player
end
Relaciona el jugador con un equipo
def relate_to_team(team_id)
@team_id = team_id
end
end
What means that error? Anybody knows what I’m doing wrong?