Ruby on rails Ruby/Rails-如何接受表单上具有多通关系的嵌套属性?
我正在rails中构建一个web应用程序,我对嵌套属性非常陌生。我正在尝试创建一个制作,我需要同时注册客户、候选人和交易参数 我发现嵌套属性是最好的解决方案,但如果将其标识为“未经许可的参数”(这是终端所说的),它就不起作用了 我用与客户、候选人和交易的关系建立了我的Facture模型,但我决定创建一个中间表,以便通过Facture和其他模型之间的关系创建has\u many 但这并不能解决问题。我想我错过了一些东西,也许我应该打电话给id,但我真的不知道怎么做 这是我的型号代码:Ruby on rails Ruby/Rails-如何接受表单上具有多通关系的嵌套属性?,ruby-on-rails,ruby,forms,simple-form,relation,Ruby On Rails,Ruby,Forms,Simple Form,Relation,我正在rails中构建一个web应用程序,我对嵌套属性非常陌生。我正在尝试创建一个制作,我需要同时注册客户、候选人和交易参数 我发现嵌套属性是最好的解决方案,但如果将其标识为“未经许可的参数”(这是终端所说的),它就不起作用了 我用与客户、候选人和交易的关系建立了我的Facture模型,但我决定创建一个中间表,以便通过Facture和其他模型之间的关系创建has\u many 但这并不能解决问题。我想我错过了一些东西,也许我应该打电话给id,但我真的不知道怎么做 这是我的型号代码: class
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
以下是我的最终行动:
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)
我错过了什么 错误“unpermitted parameters”意味着这是控制器中的问题,特别是“strong parameters”代码
我认为问题在于您为嵌套对象指定允许属性的方式
这是一个未经测试的版本,我认为你的制作参数方法应该是这样的。您可能需要进行一些测试和修改,以使其完全正确。特别是,嵌套参数是否需要包含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
谢谢你的回答!我试过了,但这并没有改变他们仍然是不被允许的。如果没有更多的信息,我无法让我的答案更精确,但请仔细阅读Rails指南,并尝试一些变体@EliseSerres执行此操作时,终端中现在显示的错误是什么?我认为这个问题可能是在你做字段的时候,在字段中,你应该做:交易、候选人和客户,而不是你单一的方式
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)
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