我在生成嵌套模型表单时遇到问题.
这是我的模特:
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
Run Code Online (Sandbox Code Playgroud)
在Workout控制器中,这是我对新动作的所有内容:
def new
@workout = Workout.new
3.times { @workout.scores.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: @wod }
end
end
Run Code Online (Sandbox Code Playgroud)
但是,在表单中,当我尝试fields_for时,我什么都没得到:
<% f.fields_for :scores do |builder| %>
<p>
<%= builder.label :score %><br />
<%= builder.text_field :score %>
</p>
<% …Run Code Online (Sandbox Code Playgroud)