0

I think I have all of this setup right for being able to send nested attributes, however, I keep getting 422 unprocessable entity but no error message. Here is how I have things configured:

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

Here is how the data looks in the schema:

  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

The log reports 422, but no errors:

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)

Raw JSON Payload

{
    "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"
    }
}

Just need another set of eyes. I'm sure I'm just missing something simple.

1 Answer 1

2

Try adding inverse_of in your associations:

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
Sign up to request clarification or add additional context in comments.

2 Comments

I thought rails was able to infer that if it wasn't explicit. Did that change in rails 5? Thanks, btw! It works!
No, you need to specify it explicit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.