Ruby on rails 以单一形式创建第三级子级-Rails 5

Ruby on rails 以单一形式创建第三级子级-Rails 5,ruby-on-rails,has-many-through,nested-form-for,ruby-on-rails-5,Ruby On Rails,Has Many Through,Nested Form For,Ruby On Rails 5,我正在ROR上构建一个简单的自上而下的日常锻炼应用程序。我可以在同一表格上创建锻炼日(家长)和锻炼日(孩子)。但是当我提交表单时,我似乎无法保存加权集(孙子)。有趣的是,由于练习已保存,我可以转到该练习编辑页面,添加一个加权集,加权集将显示在训练日显示页面中。我认为这与加权集在创建时与练习无关有关。我怎么把这三个模型连接在一起?我知道我很接近 我有全部。如果链接不起作用,请尝试此URL 模型 class WorkoutDay

我正在ROR上构建一个简单的自上而下的日常锻炼应用程序。我可以在同一表格上创建锻炼日(家长)和锻炼日(孩子)。但是当我提交表单时,我似乎无法保存加权集(孙子)。有趣的是,由于练习已保存,我可以转到该练习编辑页面,添加一个加权集,加权集将显示在训练日显示页面中。我认为这与加权集在创建时与练习无关有关。我怎么把这三个模型连接在一起?我知道我很接近

我有全部。如果链接不起作用,请尝试此URL

模型
class WorkoutDay
训练日控制器
class WorkoutDaysController@parent.child.build
@锻炼日、锻炼日、健身日
#有多个:通过关联.build方法=>@parent.through\u child.build
#@workout\u day.weighted\u set.build
@锻炼\日。加权\套。体形
结束
...
#训练后/训练日
#POST/workout_days.json
def创建
@训练日=训练日。新建(训练日参数)
回应待办事项|格式|
如果@workout\u day.save
format.html{将_重定向到@workout _day,注意:'已成功创建训练日。}
format.json{render:show,status::created,location:@workout\u day}
其他的
format.html{render:new}
format.json{render json:@workout_day.errors,status::unprocessable_entity}
结束
结束
结束
...
私有的
#使用回调在操作之间共享公共设置或约束。
def设置训练日
@workout\u day=WorkoutDay.find(参数[:id])
结束
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def训练日参数
参数要求(:锻炼\日)。允许(:标题,锻炼\属性:[:标题,:\销毁,加权\设置\属性:[:id,:重量,:重复]])
结束
结束
新运动日表格

禁止保存此训练日:
运动场就在这里 加权集字段将显示在此处
罪魁祸首是
训练日参数
。在表单中,
weighted\u set
字段嵌套在
workout\u day
下。但是在
训练日参数
中,您在
练习属性
下有
加权设置属性
,这就是您出现问题的原因。将其更改为以下将解决此问题

def workout_day_params
  params.require(:workout_day).permit(:title, exercises_attributes: [:title, :_destroy], weighted_sets_attributes: [:id, :weight, :repetition])
end
ActiveRecord::HasManyThroughCanAssociateThroughHasOne或Many‌​倒影

这是由于错误的联想。你应该考虑调整你的联想,如下面的

class WorkoutDay < ApplicationRecord
  has_many :weighted_sets, dependent: :destroy
  has_many :exercises, through: :weighted_sets

  accepts_nested_attributes_for :exercises
  accepts_nested_attributes_for :weighted_sets
end

class Exercise < ApplicationRecord
  has_many :weighted_sets, dependent: :destroy
  has_many :workout_days, through: :weighted_sets
end

class WeightedSet < ApplicationRecord
  belongs_to :exercise, optional: true
  belongs_to :workout_day, optional: true
end
class WorkoutDay
再次感谢您。我以前看到过,但我一点也没想到。然而,当我保存时,我得到一个错误
ActiveRecord::hasmanythroughcanassociatethroughasoneormanyreflection in WorkoutDaysController 35; create
其他论坛会以模型关联的方式说出来。有什么想法吗?
<%= form_for @workout_day do |workout_day_form| %>
  <% if workout_day.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(workout_day.errors.count, "error") %> prohibited this workout_day from being saved:</h2>

      <ul>
      <% workout_day.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div>
    <%= workout_day_form.label :title, 'Workout Day Name' %>
    <%= workout_day_form.text_field :title %>
  </div>


  exercise_field will go here
  <div>
    <%= workout_day_form.fields_for :exercises do |exercise_field| %>
        <%= exercise_field.label :title, 'Exercise' %>
        <%= exercise_field.text_field :title %>
    <% end %>
  </div>

  weighted_set_fields will go here
  <div>
      <%= workout_day_form.fields_for :weighted_sets do |set| %>
        <%= render 'exercises/weighted_set_fields', f: set %>
      <% end %>  
  </div>

  <div>
    <%= workout_day_form.submit %>
  </div>
<% end %>
def workout_day_params
  params.require(:workout_day).permit(:title, exercises_attributes: [:title, :_destroy], weighted_sets_attributes: [:id, :weight, :repetition])
end
class WorkoutDay < ApplicationRecord
  has_many :weighted_sets, dependent: :destroy
  has_many :exercises, through: :weighted_sets

  accepts_nested_attributes_for :exercises
  accepts_nested_attributes_for :weighted_sets
end

class Exercise < ApplicationRecord
  has_many :weighted_sets, dependent: :destroy
  has_many :workout_days, through: :weighted_sets
end

class WeightedSet < ApplicationRecord
  belongs_to :exercise, optional: true
  belongs_to :workout_day, optional: true
end