Rails 5 accepts_nested_attributes_for 使用 JSON POST 获取无法处理的实体
Rails 5 accepts_nested_attributes_for Getting Unprocessable Entity with JSON POST
我认为我拥有所有这些设置,可以发送嵌套属性,但是,我不断收到 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,但没有报错:
Started 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 is
used.", "crop_water_use_units"=>"IPD", "wind_speed"=>"12",
"report_date"=>"2017-03-25T19:41:55Z",
"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"}} (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
我认为我拥有所有这些设置,可以发送嵌套属性,但是,我不断收到 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,但没有报错:
Started 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 is used.", "crop_water_use_units"=>"IPD", "wind_speed"=>"12", "report_date"=>"2017-03-25T19:41:55Z", "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"}} (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