相关疑难解决方法(0)

让fields_for使用has_many关系

我在生成嵌套模型表单时遇到问题.

这是我的模特:

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)

ruby ruby-on-rails ruby-on-rails-3.2

5
推荐指数
1
解决办法
5328
查看次数

标签 统计

ruby ×1

ruby-on-rails ×1

ruby-on-rails-3.2 ×1