Rails On创建未定义的方法`destroy?'对于#<array:0x007feeff8abf88>

时间:2018-05-24 22:27:01

标签: ruby-on-rails

在尝试创建记录时,我收到错误“未定义的方法被破坏?”就在“if @ form_response.save”这一点,我不知道为什么。这是控制器:

class FormResponsesController < ApplicationController
before_action :authenticate_user!
before_action :initialize_form, only: [:new]

def index
    @patient = Patient.find(params[:patient_id])
    @forms = Form.all
end

def new

end

def create
    @patient = Patient.find(params[:patient_id])
    @form = Form.find(params[:form_id])
    @form_response = FormResponse.new(form_response_params)
    @form_response.patient = @patient
    @form_response.form = @form
    if @form_response.save
        flash[:success] = "Form filled out."
        redirect_to patient_forms_path
    else
        puts("Wasn't created")
        flash[:error] = @form_response.errors.full_messages.to_sentence
        # THIS ISN'T WORKING, FIX THIS
        redirect_to new_patient_form_response_path(id: params[:form_id])
    end
end

def show

end

def destroy
    @form_response = FormResponse.find(params[:id]).destroy
    flash[:success] = "Form Response Deleted."
    redirect_to patient_forms_path, notice: "Form Response Deleted."
end

private

def initialize_form
    puts("initialize_form")
    @patient = Patient.find(params[:patient_id])
    @form = Form.find(params[:id])
    @form_response = FormResponse.new()

    @form.form_sections.each_with_index do |section, index|
        @form_response.form_response_sections.build(
            form_section: section
        )
        section.form_fields.each do |field|
            @form_response.form_response_sections[index].form_response_fields.build(
                field_type: field.field_type,
                name: field.name,
                key: field.key,
                field_width: field.field_width,
                value: field.value,
                description: field.description,
                required: field.required,
                form_field: field
            )
        end
    end
end

def form_response_params
    params.require("form_response").permit(
        form_response_sections_attributes: [
            :form_section_id,
            form_response_fields_attributes: [
                :form_field_id, 
                :key, 
                :value
            ]
        ]
    )
end

FormResponse模型:

class FormResponse < ApplicationRecord
    belongs_to :patient
    belongs_to :form

    has_many :form_response_sections
    accepts_nested_attributes_for :form_response_sections
end

FormResponseSection模型

class FormResponseSection < ApplicationRecord
    belongs_to :form_response_fields
    belongs_to :form_section
    has_many :form_response_fields
    accepts_nested_attributes_for :form_response_fields
end

FormResponseField模型

class FormResponseField < ApplicationRecord
    belongs_to :form_response_section
    belongs_to :form_field
end

正如你所看到的,有两个嵌套属性,试图理解可能出现的问题......“自从被破坏后?”不应该在这一点上运行,因为它不存在。这是提交新方法时的控制台输出:

 pp/controllers/form_responses_controller.rb:20:in `create'
::1 - - [24/May/2018:15:20:39 PDT] "POST /patients/1/form_responses HTTP/1.1" 500 192571
http://localhost:3000/patients/1/form_responses/new?id=1 -> /patients/1/form_responses
Started POST "/patients/1/form_responses" for ::1 at 2018-05-24 15:20:41 -0700
Processing by FormResponsesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"zN9OLKcZRE9li1j019Mp1qkvWI8aYqhxbV2YRgPdnfNnJcAqivNtuPjxK6Vm210lGUNGuwoBFcQObRyUVebgWQ==", "form_id"=>"1", "form_response"=>{"form_response_sections_attributes"=>{"0"=>{"form_section_id"=>"1", "form_response_fields_attributes"=>{"0"=>{"form_field_id"=>"1", "value"=>""}, "1"=>{"form_field_id"=>"2", "value"=>""}, "2"=>{"form_field_id"=>"3", "value"=>""}, "3"=>{"form_field_id"=>"4", "value"=>""}, "4"=>{"form_field_id"=>"5", "value"=>""}, "5"=>{"form_field_id"=>"6", "value"=>""}}}, "1"=>{"form_section_id"=>"2", "form_response_fields_attributes"=>{"0"=>{"form_field_id"=>"7", "value"=>"0"}, "1"=>{"form_field_id"=>"8", "value"=>"0"}}}, "2"=>{"form_section_id"=>"3", "form_response_fields_attributes"=>{"0"=>{"form_field_id"=>"9", "value"=>"0"}}}}}, "commit"=>"Submit", "patient_id"=>"1"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Patient Load (0.1ms)  SELECT  "patients".* FROM "patients" WHERE "patients"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Form Load (0.1ms)  SELECT  "forms".* FROM "forms" WHERE "forms"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
   (0.1ms)  begin transaction
  FormSection Load (0.1ms)  SELECT  "form_sections".* FROM "form_sections" WHERE "form_sections"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  FormField Load (0.0ms)  SELECT  "form_fields".* FROM "form_fields" WHERE "form_fields"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  FormField Load (0.0ms)  SELECT  "form_fields".* FROM "form_fields" WHERE "form_fields"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  FormField Load (0.0ms)  SELECT  "form_fields".* FROM "form_fields" WHERE "form_fields"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
  FormField Load (0.0ms)  SELECT  "form_fields".* FROM "form_fields" WHERE "form_fields"."id" = ? LIMIT ?  [["id", 4], ["LIMIT", 1]]
  FormField Load (0.0ms)  SELECT  "form_fields".* FROM "form_fields" WHERE "form_fields"."id" = ? LIMIT ?  [["id", 5], ["LIMIT", 1]]
  FormField Load (0.0ms)  SELECT  "form_fields".* FROM "form_fields" WHERE "form_fields"."id" = ? LIMIT ?  [["id", 6], ["LIMIT", 1]]
  FormSection Load (0.2ms)  SELECT  "form_sections".* FROM "form_sections" WHERE "form_sections"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  FormField Load (0.1ms)  SELECT  "form_fields".* FROM "form_fields" WHERE "form_fields"."id" = ? LIMIT ?  [["id", 7], ["LIMIT", 1]]
  FormField Load (0.1ms)  SELECT  "form_fields".* FROM "form_fields" WHERE "form_fields"."id" = ? LIMIT ?  [["id", 8], ["LIMIT", 1]]
  FormSection Load (0.1ms)  SELECT  "form_sections".* FROM "form_sections" WHERE "form_sections"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
  FormField Load (0.1ms)  SELECT  "form_fields".* FROM "form_fields" WHERE "form_fields"."id" = ? LIMIT ?  [["id", 9], ["LIMIT", 1]]
   (0.1ms)  rollback transaction
   (0.0ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "form_responses" ("form_id", "patient_id") VALUES (?, ?)  [["form_id", 1], ["patient_id", 1]]
   (2.2ms)  rollback transaction
Completed 500 Internal Server Error in 33ms (ActiveRecord: 3.9ms)



NoMethodError (undefined method `destroyed?' for #<Array:0x007feeff8abf88>):

app/controllers/form_responses_controller.rb:20:in `create'
::1 - - [24/May/2018:15:20:41 PDT] "POST /patients/1/form_responses HTTP/1.1" 500 192571
http://localhost:3000/patients/1/form_responses/new?id=1 -> /patients/1/form_responses

0 个答案:

没有答案