Можно ли создать несколько строк в таблице объединенных моделей, используя new_form из упражнения?
Код работает только при создании одного упражнения, которое связывается с разделом body_section, а затем выбирает существующую мышцу.
Я пытался изменить код для использования check_box, но не смог
Оригинальный код
exercise.model
has_many :body_sections
has_many :muscles, through: :body_sections
accepts_nested_attributes_for :body_sections
end
muscle.model
has_many :body_sections
has_many :exercises, through: :body_sections
body_section.model
belongs_to :muscle
belongs_to :exercise
accepts_nested_attributes_for :exercise
end
Тренировочный контроллер
def new
@exercise = Exercise.new
@exercise.body_sections.build
@muscles = Muscle.all
end
# private method for strong parameter
params.require(:exercise).permit(:name, :note, :body_sections_attributes => [:name, :muscle_id])
Изменено для check_box
упражнение _form.view
<div>
<%= exercise_form.label :name, "Exercise Name" %>
<%= exercise_form.text_field :name %>
</div>
<div>
<%= exercise_form.fields_for :body_sections do |body_form| %>
<%= body_form.label :name, "Body Section Common Name" %>
<%= body_form.text_field :name %>
<br>
<%= body_form.collection_check_boxes(:muscle_ids, @muscles, :id, :name) do |c| %>
<%= c.label { c.check_box } %>
<% end %>
<% end %>
</div>
контроллер упражнений
# private method for strong parameter
params.require(:exercise).permit(:name, :note, :body_sections_attributes => [:name, :muscle_ids => []])
Я получаю неопределенную ошибку метода 'muscle_ids'
по-видимому, body_section не имеет методов muscle_ids, принадлежащих ему. Как мне изменить код, чтобы можно было использовать флажок для выбора и создания нескольких строк в body_sections одновременно ??