Rails невозможно динамическое c добавить еще одно поле ввода во вложенной форме (двухуровневой) ассоциация имеет много сквозных - PullRequest
1 голос
/ 27 мая 2020

Я работаю в Rails, и у меня проблема в формуле

Формула

enter image description here

Модели

У меня 3 модели, вероятно, с неправильной ассоциацией

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 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>

Когда я частично меняю с ":tag" на ":tags", он работает по формуле, но, к сожалению, у меня проблема с сохранением записей в базу данных ... INSERT INTO table TAGS отсутствует, у меня только INSERTS to tag_percent и ta_conditions

Можете ли вы Помоги мне, пожалуйста? Или покажите какой-нибудь учебник (или простой проект на Github) с тем же регистром (все на Youtube только с одним уровнем вложенной формы, нет моего случая имеет много сквозных с процентами поля в "средней" таблице Tag_percent и Dynami c добавить строки).

1 Ответ

0 голосов
/ 28 мая 2020

Решено! С помощью этого демонстрационного проекта https://github.com/nathanvda/cocoon_simple_form_demo

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...