I had a problem creating a nested model.
Here are my models:
class Workout < ActiveRecord::Base
has_many :scores
has_many :users, :through => :scores
accepts_nested_attributes_for :scores
end
class Score < ActiveRecord::Base
belongs_to :user
belongs_to :workout
end
class User < ActiveRecord::Base
has_many :scores
has_many :workout, :through => :scores
end
In the workout controller, here is what I have for a new action:
def new
@workout = Workout.new
3.times { @workout.scores.build }
respond_to do |format|
format.html
format.json { render json: @wod }
end
end
However, in the form, when I try fields_for, I get nothing:
<% f.fields_for :scores do |builder| %>
<p>
<%= builder.label :score %><br />
<%= builder.text_field :score %>
</p>
<% end %>
What am I doing wrong?
source
share