通过嵌套属性从另一个控制器通过表单编辑创建数据

时间:2017-10-31 15:32:21

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 nested-attributes

我尝试通过编辑表单为其他控制器中的操作添加值。我有以下型号:

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 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 DaySalesman < ApplicationRecord
  belongs_to :day
  belongs_to :salesman

  accepts_nested_attributes_for :salesman
end

换句话说,我有一天可以有很多员工,很多员工都可以参与其中。

当我编辑这一天时,我希望可以通过day_salesman表添加员工并将其关联到我的一天。

我试图这样做,但是我收到以下错误日志:

ActionController::ParameterMissing (param is missing or the value is empty: salesman):

app/controllers/salesmen_controller.rb:49:in `params_salesman'
app/controllers/salesmen_controller.rb:17:in `create'
  Rendering /box/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /box/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /box/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (32.8ms)
  Rendering /box/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /box/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (28.5ms)
  Rendering /box/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /box/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (53.3ms)
  Rendered /box/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (216.0ms)
Started POST "/companies/3/salesmen" for 172.24.0.1 at 2017-10-31 15:21:09 +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"=>"/5ULp1WOehoaJIZL0SaCSDYU9MssS7ZQ5EfyTkmZCyFSvogj6lOtxOTuNTx8AjdeRjAnkkd3XhD5V30/QAXijg==", "day"=>{"value"=>"400", "day_salesman"=>{"salesman"=>{"name"=>"Denis"}}},"commit"=>"Create","company_id"=>"3"}
  [1m[36mOwner Load (3.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 108ms (ActiveRecord: 12.7ms)

我的控制器是:

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

DaysController:

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

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

<%= 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| %>
    <%= ff.fields_for :salesman do |fff| %>
      <%= fff.label :names_of_salesmen %>
      <%= fff.text_field :name %>
    <% end %>
  <% end %>
  <%= f.submit "Create" %>
<% end %>

我的路线是:

Rails.application.routes.draw do
  root to: 'companies#index'
  resources :companies do
    resources :salesmen
    resources :goals do
      resources :days
    end
  end
  devise_for :owners, :controllers => { registrations: 'registrations' }
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

我试图使用嵌套属性,但似乎我采用了错误的方法,有人可以帮助您通过日期的编辑形式获得推销员并将其联系起来吗?

2 个答案:

答案 0 :(得分:1)

我会尝试替换

<%= f.fields_for :salesman do |fff| %>

使用:

<%= ff.fields_for :salesman do |fff| %>

获取正确的表单构建器。

答案 1 :(得分:1)

salesmen_controller.rb中应该是这样的,我不确定你在强参数中使用了什么。请尝试按顺序进行修改

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