0

我认为我拥有能够发送嵌套属性的所有这些设置,但是,我不断收到 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,但没有错误:

在 2017-03-25 13:41:55 -0600 开始 POST "/scouting_reports.json" for 127.0.0.1 由 ScoutingReportsController#create as JSON 参数处理:{"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" =>“看起来就在那里”、“动作”=>“治疗”、“无序”=>“”、“lon”=>“-97.989378”、“level”=>“3”、“lat”=> "40.875492", "disorder_id"=>158}], "wind_speed_degree"=>"10", "天空"=>"B", "crop_growth_stage"=>"Stage 1", "temperature"=>"68", "wind_speed_direction"=>"NW", "irrigation_comment"=>"如果没有受到刺激就灌溉作物", " stand_count"=>"12", "crop_condition_comments"=>"Comment about the conditions", "crop_id"=>"1234"}} (0.1ms) BEGIN (0.1ms) ROLLBACK Completed 422 Unprocessable Entity in 33ms (Views: 0.2毫秒 | 活动记录:4.4 毫秒)"12", "crop_condition_comments"=>"Comment about the conditions", "crop_id"=>"1234"}} (0.1ms) BEGIN (0.1ms) ROLLBACK Completed 422 Unprocessable Entity in 33ms (Views: 0.2ms | ActiveRecord: 4.4毫秒)"12", "crop_condition_comments"=>"Comment about the conditions", "crop_id"=>"1234"}} (0.1ms) BEGIN (0.1ms) ROLLBACK Completed 422 Unprocessable Entity in 33ms (Views: 0.2ms | ActiveRecord: 4.4毫秒)

原始 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"
    }
}

只是需要另一双眼睛。我确定我只是缺少一些简单的东西。

4

1 回答 1

2

尝试添加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
于 2017-03-26T05:37:24.890 回答