我到处搜索,但没有找到任何解决方案。我使用Rails 4.1 nested_form与单表继承。
我有模型 - suspects
和victims
继承自模型person
。
每个alert
都有suspects
个victims
个alert
在提交时,它只将alert
数据填充到数据库中,而不是其nested_attributes。请参阅型号名称Started POST "/alerts" for 127.0.0.1 at 2014-04-25 13:50:26 -0500
Processing by AlertsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4RHZ6i0eWIWnyYwmick7VlmGouTRiTFciKiQUw/Ea54=", "alert"=>{"victims_attributes"=>{"0"=>{"first_name"=>"", "last_name"=>"", "race"=>"", "gender"=>"", "hair_color"=>"", "eye_color"=>"", "height_ft"=>"", "height_inch"=>"", "age"=>"", "age_unit"=>"", "birth_date"=>"__/__/____", "weight"=>"", "additional_info"=>"", "_destroy"=>"false"}}, "created_at"=>"____/__/__ __:__", "nic"=>"", "investigating_officer_name"=>"", "investigating_officer_title"=>"", "investigating_officer_phone"=>"", "agency_name"=>"", "agency_phone"=>"", "reporting_agency_phone"=>"", "reporting_agency_email"=>""}, "commit"=>"Create Alert"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 8 ORDER BY "users"."id" ASC LIMIT 1
(0.0ms) BEGIN
SQL (2.0ms) INSERT INTO "alerts" ("agency_name", "agency_phone", "created_at", "investigating_officer_name", "investigating_officer_phone", "investigating_officer_title", "nic", "reporting_agency_email", "reporting_agency_phone", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["agency_name", ""], ["agency_phone", ""], ["created_at", "2014-04-25 18:50:26.422890"], ["investigating_officer_name", ""], ["investigating_officer_phone", ""], ["investigating_officer_title", ""], ["nic", ""], ["reporting_agency_email", ""], ["reporting_agency_phone", ""], ["updated_at", "2014-04-25 18:50:26.422890"]]
(62.0ms) COMMIT
Redirected to http://127.0.0.1:3000/alerts
Completed 302 Found in 87ms (ActiveRecord: 66.0ms)
及其型号。
已删除作者链接
我得到的是:
SQL (2.0ms) INSERT INTO "people" ("additional_info", "age_unit", "alert_id", "created_at", "eye_color", "first_name", "gender", "hair_color", "last_name", "race", "type", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["additional_info", ""], ["age_unit", ""], ["alert_id", 16], ["created_at", "2014-04-25 19:27:29.080890"], ["eye_color", ""], ["first_name", ""], ["gender", ""], ["hair_color", ""], ["last_name", ""], ["race", ""], ["type", "victim"], ["updated_at", "2014-04-25 19:27:29.080890"]]
SQL (2.0ms) INSERT INTO "vehicle" ("additional_info", "age_unit", "alert_id", "created_at", "eye_color", "first_name", "gender", "hair_color", "last_name", "race", "type", "updated_at") .....
但我也期待它的nested_attributes(使用STI)嫌疑人,车辆和受害者也在查询,但它没有发生。
任何帮助/建议都会欣赏。
我希望此查询能够显示两次One for Victim和一次显示为Suspect。我没有看到日志上的任何错误。
{{1}}
答案 0 :(得分:3)
这是因为您传递了额外的form
标记,请检查源代码。您不应在form
内创建fields_for
标记。