1

在我的应用程序中,我有一个目标,这个目标属于一家公司,并且有开始和结束日期,当它被自动创建时,在目标中定义的开始日期和结束日期之间的日期表中创建值,这个字段的名称为 date_day。

除了这个字段每个Day还有一个value字段,即当天收集的总金额,这个值在用户创建目标时不会自动定义,即为null,需要编辑Day来定义如何那天收集了很多。当我去做当天的版本时,我想在我的模型推销员中添加一名员工,该员工将与那天绑定。

我通过 Day 编辑表单添加了一名员工,但仅此而已,day 值的版本没有更新,而且 DaySalesman 表也没有增量。

我的模型:

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

以下是我的控制器和我的控制器的结果:

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

推销员:

    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

我的控制器日视图编辑是:

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

当我单击此视图上的提交按钮时,我的日志是:

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)

我想知道如何通过这一天的编辑表单更新这一天的值,创建一个新员工并通过 Daysalesmen 表将其关联到这一天。

4

0 回答 0