For a month now, trying to solve the problem at a glance, itβs not very difficult: There are 3 models - team, user and team_user (has_namy: through) In the editing form and a new team to dynamically add members to this team.
Scenario:
- The user comes in a new team form.
- Designates a team name
- After selecting the username (team member) field
- Click the Add Member button
- After confirmation of membership is added after the team name field in the text field + delete button opposite
- Remove its name (member) from the selector (since this is already a member of the team)
- In selite selects the next user and click "Add Member"
- Click the Submit button to save the new team and team members.
Difficulties:
- I tried making through the Cocoon gem, but it is impossible to make another parshaly to select the user to be added to it (SELECT), and added the participants (full name - text).
- If this is done via <% = from_for ... remote: true%> and a separate controller or a new action in the teams_controller controller, these will be two sub-forms (team forms and team_user forms) with its second submission button. Attachment forms, as I understand it, are not buzzing.
- Changes in the form (changing the team name and adding / removing team members have an account only after clicking on save to basically send the form command).

application / models / user.rb
class User < ApplicationRecord has_many :team_users has_many :teams, through: :team_users accepts_nested_attributes_for :team_users, :teams, allow_destroy: true end
application / models / team.rb
class Team < ApplicationRecord has_many :team_users has_many :users, through: :team_users accepts_nested_attributes_for :team_users, allow_destroy: true, reject_if: proc { |a| a['user_id'].blank? } end
application / models / team_user.rb
class TeamUser < ApplicationRecord belongs_to :team belongs_to :user accepts_nested_attributes_for :team, :user, allow_destroy: true end
application / controllers / teams_controller.rb
class TeamsController < ApplicationController before_action :set_team, :set_team_users, only: [:show, :edit, :update, :destroy] before_action :set_team_ancestry, only: [:new, :edit, :create, :update, :destroy] before_action :set_new_team_user, only: [:new, :edit] before_action :logged_in_user layout 'sidebar'