vendredi 8 juillet 2022

Record Unable to save in join table using has_many through relationship in Ruby on Rails

I am trying to create an Employee team app using join table has_many through relationship

In the team_leader column, I am going to store one employee_name and in the team_employee_name column going to store the number of employee_name using the check box. pls, help me I am stuck. Thanks

Migration

team Migration Table
 
 t.string :team_leader
 t.string :team_employee_name
 

Employee Migration Table
 
  t.string :employee_name

TeamEmployee Migration table

  t.references :team, null: false, foreign_key: true
  t.references :employee, null: false, foreign_key: true

Model

    model team

    has_many :team_employees
    has_many :teams, through: :team_employees
    
    model Employee
    
    has_many :team_employees
    has_many :employees, through: :team_employees
    
    model TeamEmployee 
    
    belongs_to :team
    belongs_to :employee

teams/index.html.erb

<%= simple_form_for(@team) do |f| %>
    <div class="field">
     #going to store one employee_name in t.string :team_leader colume
    <%= f.label "Add Leader"%>
    <%= f.select :team_employees, Employee.pluck(:employee_name)%>
     #going to store the number of employee_name using the check box in t.string :team_employee_name colume
    <%= f.label "Add users to team" %><br />
    <%= f.collection_check_boxes :team_employees, Employee.all, :id, :employee_name do |b| %>
        <div class="collection-check-box">
          <%= b.check_box %>
          <%= b.label %>
        </div>
  <% end %>
  
    <%= f.button :submit%>

  <% end %>

teams/controller.rb

class TeamsController < ApplicationController

before_action :find_team_member, only: [:show, :edit, :update, :destroy]
 def index
   @team = Team.new 
end 

 def create
    @team = Team.create(team_params)
    @team.save
    redirect_to teams_path
 end

private
  
 def find_tem_member
    @team = Team.find(params[:id])
 end

 def team_params
    params.require(:team).permit(team_employees_attributes:[{employee_attributes: [:employee_name]}],employee_id:[])
 end

end

seems I made mistake in this method pls let know

def team_params
   params.require(:team).permit(team_employees_attributes:[{employee_attributes: [:employee_name]}],employee_id:[])
end

Aucun commentaire:

Enregistrer un commentaire