Ruby on rails Rails不可能动态添加另一个嵌套形式的输入字段(两级)关联有多个
我在Rails工作,我在公式中遇到了一个问题 公式 模型 我有3个可能与错误关联的模型Ruby on rails Rails不可能动态添加另一个嵌套形式的输入字段(两级)关联有多个,ruby-on-rails,Ruby On Rails,我在Rails工作,我在公式中遇到了一个问题 公式 模型 我有3个可能与错误关联的模型 class TagCondition < ApplicationRecord has_many :tag_percents has_many :tags, through: :tag_percents accepts_nested_attributes_for :tag_percents end class TagPercent < ApplicationRecord belo
class TagCondition < ApplicationRecord
has_many :tag_percents
has_many :tags, through: :tag_percents
accepts_nested_attributes_for :tag_percents
end
class TagPercent < ApplicationRecord
belongs_to :tag_condition, optional: true
belongs_to :tag, optional: true
accepts_nested_attributes_for :tag
end
class Tag < ApplicationRecord
belongs_to :tag_percent, dependent: :destroy
end
class TagCondition
控制器
class标记条件控制器
看法
...
建筑商%>
...
在部分_标记_百分比中。。。
当我将部分从“:tag”
更改为“:tags”
时,它在公式中工作,但不幸的是,我在将记录保存到数据库时遇到问题<代码>插入到表中缺少标记,我只有插入到标记的百分比
和ta\u条件
你能帮我吗?或者使用相同的案例显示一些教程(或Github上的一个简单项目)(Youtube中的所有内容都只有一个级别的嵌套表单没有my case在“middle”表标记中有多个字段百分比和动态添加行)。已解决!
在这个演示项目的帮助下
解决了!
在这个演示项目的帮助下
class TagConditionsController < ApplicationController
before_action :set_tag_condition, only: [:show, :edit, :update, :destroy]
def new
@tag_condition = TagCondition.new
per = @tag_condition.tag_percents.build
per.build_tag (maybe wrong, but "per.tags.build" get error)
end
def tag_condition_params
params.require(:tag_condition).permit(
:condition,
tag_percents_attributes: [ :id, :percent,
{tag_attributes: [ :id, :name]}] )
end
<%= form_for(tag_condition) do |f| %>
<div class="field">
<%= f.label :condition %>
<%= f.text_field :condition %>
</div>
...
<tbody class="fields">
<%= f.fields_for :tag_percents do |builder| %>
<%= render 'tag_percent_fields' , :f => builder %>
<% end %>
</tbody>
...
and in partial _tag_percent...
<td>
<%= f.fields_for **:tag** do |builder| %>
<%= builder.text_field :name, class: "form-control" %>
<% end %>
</td>
<td>
<%= f.number_field :percent, in: 1..100, placeholder: 100, class: "form-control" %>
</td>