중첩 된 속성을 보낼 수있는이 모든 설정 권한이 있다고 생각하지만 처리 할 수없는 엔티티 422가 계속 표시되지만 오류 메시지는 표시되지 않습니다. 구성하는 방법은 다음과 같습니다.
scouting_report.rb
class ScoutingReport < ApplicationRecord
has_many :scouting_report_details
accepts_nested_attributes_for :scouting_report_details, :allow_destroy => true
end
scouting_report_detail.rb
class ScoutingReportDetail < ApplicationRecord
belongs_to :scouting_report
end
scouting_reports_controller.rb
def scouting_report_params
params.require(:scouting_report).permit(
:customer_id,
:report_date,
:crop_id,
:wind_speed,
:wind_speed_direction,
:wind_speed_degree,
:temperature,
:sky,
:crop_growth_stage,
:crop_condition_comments,
:stand_count,
:irrigation_comment,
:crop_water_use,
:crop_water_use_units,
scouting_report_details_attributes: [
:id,
:action,
:disorder_id,
:disorder,
:identifiaction,
:lon,
:level,
:lat,
:scouting_report_id])
end
다음은 스키마에서 데이터가 표시되는 방식입니다.
create_table "scouting_reports", force: :cascade do |t|
t.integer "customer_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "report_date"
t.integer "crop_id"
t.string "wind_speed"
t.string "wind_speed_direction"
t.string "wind_speed_degree"
t.string "temperature"
t.string "sky"
t.string "crop_growth_stage"
t.text "crop_condition_comments"
t.string "stand_count"
t.text "irrigation_comment"
t.string "crop_water_use"
t.string "crop_water_use_units"
end
create_table "scouting_report_details", force: :cascade do |t|
t.string "disorder"
t.integer "disorder_id"
t.string "level"
t.string "action"
t.string "identifiaction"
t.string "lat"
t.string "lon"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "scouting_report_id"
end
로그는 422를보고하지만 오류는 없습니다.
시작됨 POST "/scouting_reports.json"for 127.0.0.1 at 2017-03-25 13:41:55 -0600 Processing by ScoutingReportsController # create as JSON Parameters : { "scouting_report"=> { "crop_water_use"=> "Yes. Water 사용됩니다. ","crop_water_use_units "=>"IPD ","wind_speed "=>"12 ","report_date "=>"2017-03-25T19 : 41 : 55Z ","scouting_report_details_attributes "=> [{"identifiaction " => "거기있는 것 같습니다", "action"=> "Treat", "disorder"=> "", "lon"=> "-97.989378", "level"=> "3", "lat"=> "40.875492", "disorder_id"=> 158}], "wind_speed_degree"=> "10", "sky"=> "B ","crop_growth_stage "=>"1 단계 ","온도 "=>"68 ","wind_speed_direction "=>"NW ","irrigation_comment "=>"작물은 자극을받지 않으면 관개됩니다. ","stand_count "= > "12", "crop_condition_comments"=> "조건에 대한 설명", "crop_id"=> "1234"}} (0.1ms) BEGIN (0.1ms) ROLLBACK 완료 33ms 만에 422 처리 할 수없는 엔티티 (보기 : 0.2ms | ActiveRecord : 4.4ms)"Comment about the conditions", "crop_id"=> "1234"}} (0.1ms) BEGIN (0.1ms) ROLLBACK Completed 422 Unprocessable Entity in 33ms (Views : 0.2ms | ActiveRecord : 4.4ms)"Comment about the conditions", "crop_id"=> "1234"}} (0.1ms) BEGIN (0.1ms) ROLLBACK Completed 422 Unprocessable Entity in 33ms (Views : 0.2ms | ActiveRecord : 4.4ms)
원시 JSON 페이로드
{
"scouting_report" : {
"crop_water_use" : "Yes. Water is used.",
"crop_water_use_units" : "IPD",
"wind_speed" : "12",
"report_date" : "2017-03-25T14:45:10Z",
"scouting_report_details_attributes" : [
{
"identifiaction" : "Looks like it's there",
"action" : "Treat",
"disorder" : "",
"lon" : "-97.989378",
"level" : "3",
"lat" : "40.875492",
"disorder_id" : 158
}
],
"wind_speed_degree" : "10",
"sky" : "B",
"crop_growth_stage" : "Stage 1",
"temperature" : "68",
"wind_speed_direction" : "NW",
"irrigation_comment" : "The crops are irrigated if not irritated",
"stand_count" : "12",
"crop_condition_comments" : "Comment about the conditions",
"crop_id" : "1234"
}
}
다른 눈이 필요합니다. 나는 단순한 것을 놓치고 있다고 확신합니다.
inverse_of
연결을 추가해보세요 .
class ScoutingReport < ApplicationRecord
has_many :scouting_report_details, inverse_of: :scouting_report
end
class ScoutingReportDetail < ApplicationRecord
belongs_to :scouting_report, inverse_of: :scouting_report_details
end
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다