nested attributes through

2

I own 3 models:

class Day < ApplicationRecord
  belongs_to :goal
  has_many :day_salesmen, dependent: :destroy
  has_many :salesmen, through: :day_salesmen
  validates_presence_of :date_day, :goal_id

  accepts_nested_attributes_for :day_salesmen
end

class DaySalesman < ApplicationRecord
  belongs_to :day
  belongs_to :salesman

  accepts_nested_attributes_for :salesman

end

class Salesman < ApplicationRecord
  belongs_to :company
  has_many :goal_salesmen, dependent: :destroy
  has_many :goals, through: :goal_salesmen

  has_many :day_salesmen, dependent: :destroy
  has_many :days, through: :day_salesmen

end

In my application I have a goal, this goal belongs to a company and has start and end date, when it is created automatically values are created in the day table that go between the start date and end date defined in the goal, this field is called date_day.

In addition to this field each Day has a value field, which is the total amount collected during the day, this value is not automatically defined when the user creates a goal, ie it comes null, it is necessary to edit the Day to define the how much was collected that day. When I do the day edition I would like to add an employee in my Salesman model, this employee would be tied to that Day.

I've managed to add an employee through the Day edit form, but that's it, day value editing is not updated and I also do not have DaySalesman tables increment.

Below are my controllers and the result of my controllers:

class DaysController < ApplicationController
  before_action :find_day, only: [:show, :edit, :update]
  before_action :find_company, only: [:show, :edit]

  def index
    @day = current_owner.companies.find(params[:company_id]).goal.find(params[:goal_id]).days
  end

  def show
  end

  def edit
    @dayup = Day.new
    @day_salesmen = @dayup.day_salesmen.build
    @salesman = @day_salesmen.build_salesman
  end

  def update
    if @day.update(params_day)
      flash[:notice] = "Day updated!"
      redirect_to company_salesman_path(:id => @day.id)
    else
      flash.now[:error] = "Could not update day!"
      render :edit
    end
  end

  private

  def find_company
    @company = Company.find(params[:company_id])
  end

  def find_day
    @day = Day.find(params[:id])
  end

  def params_day
    params.require(:day).permit(:value, day_salesman_attributes: [:id, salesman_attributes:[:id, :name]]).merge(goal_id: params[:goal_id])
  end
end

Salesman:

class SalesmenController < ApplicationController
  before_action :find_salesman, only: [:edit, :update, :destroy]

  def index
    @salesmen = current_owner.companies.find(params[:company_id]).salesman
    @company = Company.find(params[:company_id])
  end


  def create
    @salesman = Salesman.new(params_salesman)
    if @salesman.save
      flash[:notice] = "Salesman saved!"
    else
      flash.now[:error] = "Cannot create salesman!"
      render :new
    end
  end

  def update
    if @salesman.update(params_salesman)
      flash[:notice] = "salesman updated!"
    else
      flash.now[:error] = "Could not update salesman!"
      render :edit
    end
  end

  def destroy
    @salesman.destroy
  end

  private

  def find_salesman
    @salesman = Salesman.find(params[:id])
  end

  def params_salesman
    params.require(:day).require(:salesman).permit(:name, :id).merge(company_id: params[:company_id])
  end
end

My view edit of controller day is:

<%= render "shared/sidebar2" %>
<div class="container">
  <div class="row">
    <div class="col s10 offset-s2">
      <div class="row">
        <h4>Edit day</h4>
        <%= form_for(@dayup, url: company_salesmen_path) do |f| %>
        <%= f.label :value_of_day %>
        <%= f.number_field :value %>
        <%= f.fields_for :day_salesman do |ff| %>
        <%= f.fields_for :salesman do |fff| %>
        <%= fff.label :names_of_salesmen %>
        <%= fff.text_field :name %>
        <% end %>
        <% end %>
        <%= f.submit "Create" %>
        <% end %>
      </div>
    </div>
  </div>
</div>

My log when I click the submit button on this view is:

Started POST "/companies/1/salesmen" for 172.26.0.1 at 2017-10-31 22:11:56 +0000
Cannot render console from 172.26.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SalesmenController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"CQaRlVPYsPK8ilOQJCdw04htgXFkUQOKEVsOIDAmRXOM1X3QO+HfOzY2PGnmItMTK9jRj0YFZDYRJPg7qBV27A==", "day"=>{"value"=>"100", "salesman"=>{"name"=>"Renata"}}, "commit"=>"Create", "company_id"=>"1"}
  [1m[36mOwner Load (0.7ms)[0m  [1m[34mSELECT  "owners".* FROM "owners" WHERE "owners"."id" = $1 ORDER BY "owners"."id" ASC LIMIT $2[0m  [["id", 1], ["LIMIT", 1]]
  [1m[35m (12.4ms)[0m  [1m[35mBEGIN[0m
  [1m[36mCompany Load (0.4ms)[0m  [1m[34mSELECT  "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT $2[0m  [["id", 1], ["LIMIT", 1]]
  [1m[35mSQL (111.1ms)[0m  [1m[32mINSERT INTO "salesmen" ("name", "company_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"[0m  [["name", "Renata"], ["company_id", 1], ["created_at", "2017-10-31 22:11:56.344478"], ["updated_at", "2017-10-31 22:11:56.344478"]]
  [1m[35m (36.3ms)[0m  [1m[35mCOMMIT[0m
No template found for SalesmenController#create, rendering head :no_content
Completed 204 No Content in 436ms (ActiveRecord: 161.0ms)

I wanted to know how through this day's edit form I can update the value for day, create a new employee and associate it with that day through the Daysalesmen table.

    
asked by anonymous 31.10.2017 / 23:14

0 answers