嵌套属性有很多通过

时间:2017-10-31 08:45:31

标签: ruby-on-rails nested-attributes has-many-through accepts-nested-attributes

我有3个型号:

class Day < ApplicationRecord
  belongs_to :goal
  has_many :day_salesmen, dependent: :destroy
  has_many :salesmen, through: :day_salesman
  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

我想在编辑Days时在我的表中添加销售人员,并通过day_salesman表关联日期和销售人员。我正在努力做到这一点,但我没有做对,我正在发送我的控制器和我的观点:

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

  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_day
    @day = Day.find(params[:id])
  end

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

推销员控制器:

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

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

  def new

  end

  def show

  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 edit
  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(:salesman).permit(:name).merge(company_id: params[:company_id])
  end
end

天编辑视图:

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

当我填写编辑表单时,它会出现以下错误:

错误记录:

Started POST "/companies/3/salesmen" for 172.24.0.1 at 2017-10-31 11:51:47 +0000
Cannot render console from 172.24.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"=>"xQgslyRRkHIEab/E+vEQDYJhxg28IoBNNqV7N8fJxT5oI68Tm4xHrPqjDLNX1aUb8kUVVNceaA0rtfRGzlUskQ==", "day"=>{"value"=>"500", "salesman"=>{"name"=>"Denis"}}, "commit"=>"Create", "company_id"=>"3"}
  [1m[36mOwner Load (0.5ms)[0m  [1m[34mSELECT  "owners".* FROM "owners" WHERE "owners"."id" = $1 ORDER BY "owners"."id" ASC LIMIT $2[0m  [["id", 1], ["LIMIT", 1]]
Completed 400 Bad Request in 28ms (ActiveRecord: 0.5ms)

我不知道我是否做得对,可能不是,记得我在编辑这一天时想要添加推销员,而不是在我创造这一天时,我不知道这是否可能。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

通常在这种情况下,我会检查服务器日志,以查看警告错误

逻辑是:

  1. 对象被实例化

    @day = Day.new input_params
    
  2. 在保存

    之前验证对象
    @day.valid?
    
  3. 保存对象

    @day.save
    
  4. 如果未保存对象,则原因是:

    您的参数有问题

    @day = Day.new input_params
    

    因此您的input_params遗漏了某些内容或格式错误,请在使用params保存代码之前停止检查binding.pry,并比较params的值与input_params

    这是an example如何传递带有嵌套表格的input_params

    {
      'person' => {
        'name' => 'John Doe',
        'addresses_attributes' => {
          '0' => {
            'kind' => 'Home',
            'street' => '221b Baker Street'
          },
          '1' => {
            'kind' => 'Office',
            'street' => '31 Spooner Street'
          }
        }
      }
    }
    

    您错过了一个对象

    例如,您正在保存与另一个对象具有belongs_to关系的对象,但该对象不存在...这种情况

    其他一些原因难以弄明白