got a problem with one to one relations
I have some Matches and i want to have one score for a match.
my Match.rb
has_one :score, :dependent => :destroy
my score.rb
belongs_to :match
my scores_controller.rb
def new
@match = Match.find(params[:match_id])
@score = @match.score.new
end
def create
@match = Match.find(params[:match_id])
@score = @match.score.create(params[:score])
end
my routes.rb
resources :matches do
resources :scores
end
my scores/new.html.haml
= form_for([@match, @match.score.build]) do |f|
= f.label :score1
= f.text_field :score1
%br
= f.label :score2
=f.text_field :score2
%br
= f.submit
my error that i get
undefined method `new' for nil:NilClass
i haven't worked with one to one relations so far since I'm pretty new to RoR, any suggestions?
EDIT
edited my code to match create_score and build_score, seems to work. but now i have some kind of strange behavior.
in my score.rb
attr_accessible :score1, :score2
but when i try to invoke in my matches/show.html.haml
= @match.score.score1
i get an unknown method call or i don't see anything at all... but if i just call
= @match.score
i get an score object returned (e.g. #)#
EDIT 2
Fix'd problem. I was calling
scores/new.haml.html
= form_for([@match, @match.create_score])
needs to be
= form_for([@match, @match.build_score])
everything works as intended.
needed to enter rails console and fetch those objects to see every :score1 :score2 was nil