我正在Rails中构建一个Web应用程序,并且我对嵌套属性非常陌生。我正在尝试创建一个工厂,并且需要同时注册“客户”,“候选人”和“交易”参数。
我发现嵌套属性是最好的解决方案,但是不能识别为“不允许的参数”(终端就是这样)。
我通过与客户,候选人和交易的belongs_to关系来构建我的Facture模型,但是我决定创建一个中间表,以便通过Facture和其他模型之间的关系来创建has_many。
但是它不能解决问题。我想我可能想念ID,但我真的不知道该怎么做。
这是我的模型代码:
class Facture < ApplicationRecord
belongs_to :delais_paiement
belongs_to :service
belongs_to :marche
belongs_to :team
belongs_to :status_facturation
belongs_to :condition_facturation
has_many :candidats, through: :informations_factures
has_many :clients, through: :informations_factures
has_many :deals, through: :informations_factures
has_many :informations_factures
accepts_nested_attributes_for :candidats
accepts_nested_attributes_for :informations_factures
accepts_nested_attributes_for :clients
accepts_nested_attributes_for :deals
end
class Deal < ApplicationRecord
belongs_to :client
has_many :factures, through: :informations_factures
has_many :informations_factures
accepts_nested_attributes_for :factures
end
class Client < ApplicationRecord
has_many :factures, through: :informations_factures
has_many :informations_factures
end
class Candidat < ApplicationRecord
has_many :factures, through: :informations_factures
has_many :informations_factures
end
class InformationsFacture < ApplicationRecord
belongs_to :candidat
belongs_to :client
belongs_to :deal
belongs_to :facture
end
这是我的表格:
<%= simple_form_for @facture do |f| %>
<div class="form-group">
<%= f.input :salaire, label: "Salaire"%>
</div>
<div class="form-group">
<%= f.input :condition_facturation_id, label: "Condition de facturation", collection: ConditionFacturation::statuses.keys %>
</div>
<div class="form-group">
<%= f.input :service_id, label: "Service", collection: Service::kinds.keys%>
</div>
<div class="form-group">
<%= f.input :team_id, label: "Team", collection: @team%>
</div>
<div class="form-group">
<h4>Candidat</h4>
<%= f.simple_fields_for :candidat do |af| %>
<%= af.input :name, label: "Name" %>
<%= af.input :id_janus, label: 'Id Janus' %>
<% end %>
</div>
<div class="form-group">
<h4>Client</h4>
<%= f.simple_fields_for :client do |af| %>
<%= af.input :name, label: "Name" %>
<% end %>
</div>
<div class="form-group">
<h4>Deal infos</h4>
<%= f.simple_fields_for :deal do |af| %>
<%= af.input :start_date, label: "Start Date" %>
<%= af.input :deal_date, label: "Deal Date" %>
<% end %>
</div>
<%= f.submit class: 'btn btn-primary' %>
<% end %>
这是我的Facture Controller参数代码:
private
def facture_params
params.require(:facture).permit(
:condition_facturation_id, :service_id, :team_id,
:salaire, {:client_id => [{:clients_attributes => [:name, :id]}]}, {:deals_attributes => [:deal_date, :start_date]}, {:candidats_attributes => [:name, :id_janus, :id]})
end
这是我的最终操作:
Started POST "/factures" for 127.0.0.1 at 2019-02-14 17:14:24 +0100
Processing by FacturesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mOmAyCZZwnG1RF7p9KSxMoNCgqxF6vVriC0YgjHAdS26pBIs73OKuhqkQurU0HVr8cotdRmUWjXFAeb+4ISjsA==", "facture"=>{"salaire"=>"1", "condition_facturation_id"=>"au_succes", "service_id"=>"recrutement", "team_id"=>"2", "candidat"=>{"name"=>"Kévin", "id_janus"=>"#123456"}, "client"=>{"name"=>"Jean-luc"}, "deal"=>{"start_date"=>"12/28/19", "deal_date"=>"12/29/19"}}, "commit"=>"Create Facture"}
Unpermitted parameters: :candidat, :client, :deal
(0.2ms) BEGIN
↳ app/controllers/factures_controller.rb:25
Service Load (0.7ms) SELECT "services".* FROM "services" WHERE "services"."id" = $1 LIMIT $2 [["id", 0], ["LIMIT", 1]]
↳ app/controllers/factures_controller.rb:25
Team Load (0.2ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
↳ app/controllers/factures_controller.rb:25
ConditionFacturation Load (0.3ms) SELECT "condition_facturations".* FROM "condition_facturations" WHERE "condition_facturations"."id" = $1 LIMIT $2 [["id", 0], ["LIMIT", 1]]
↳ app/controllers/factures_controller.rb:25
(0.1ms) ROLLBACK
↳ app/controllers/factures_controller.rb:25
Rendering factures/new.html.erb within layouts/application
Rendered factures/new.html.erb within layouts/application (19.7ms)
我想念什么?
答案 0 :(得分:1)
“不允许的参数”错误表示这是Controller中的问题,特别是您的“强参数”代码。
我相信问题在于您为嵌套对象指定允许的属性的方式。
这是我认为您的facture_params方法应该是什么样的未经测试的版本。您可能需要进行一些测试和修改才能使其完全正确。尤其是,嵌套参数是否需要包含id字段取决于您是否只为创建新对象或更新现有对象而接受参数。
def facture_params
params.require(:facture).permit(
:condition_facturation_id,
:service_id,
:team_id,
:salaire,
:clients_attributes => [:name, :id],
:deals_attributes => [:deal_date, :start_date],
:candidats_attributes => [:name, :id_janus, :id]
)
end