Error "must exist" when creating registration in Rails 5.2

Asked

Viewed 60 times

0

I’m creating a Teacher model that has a contact:

ActiveAdmin.register Teacher do
  permit_params :name, :contact_id

form do |f|
    f.semantic_errors *f.object.errors.keys

    f.inputs "Details" do
      f.input :name
      f.input :contact_id, :as => :select, :collection => Contact.all.collect {|contact| [contact.email] }
    end
    f.actions
  end

end

here is the model contact

ActiveAdmin.register Contact do
  permit_params :phone, :email
end

Here is the schema:

ActiveRecord::Schema.define(version: 2019_11_12_175353) do

  create_table "active_admin_comments", force: :cascade do |t|
    t.string "namespace"
    t.text "body"
    t.string "resource_type"
    t.integer "resource_id"
    t.string "author_type"
    t.integer "author_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id"
    t.index ["namespace"], name: "index_active_admin_comments_on_namespace"
    t.index ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id"
  end

  create_table "admin_users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_admin_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true
  end

  create_table "contacts", force: :cascade do |t|
    t.string "phone"
    t.string "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "groups", force: :cascade do |t|
    t.string "description"
    t.integer "teacher_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["teacher_id"], name: "index_groups_on_teacher_id"
  end

  create_table "students", force: :cascade do |t|
    t.string "name"
    t.integer "teacher_id"
    t.integer "contact_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["contact_id"], name: "index_students_on_contact_id"
    t.index ["teacher_id"], name: "index_students_on_teacher_id"
  end

  create_table "subjects", force: :cascade do |t|
    t.string "description"
    t.integer "teacher_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["teacher_id"], name: "index_subjects_on_teacher_id"
  end

  create_table "teachers", force: :cascade do |t|
    t.string "name"
    t.integer "contact_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["contact_id"], name: "index_teachers_on_contact_id"
  end

end

I’m having a contact error must exist even with contact data appearing on Teacher.

Ruby 2.4, active admin and wicked_pdf, and sqlite, if relevant.

I’ve tried to https://github.com/rails/rails/issues/24518 but does not work, returns another error.

1 answer

0


You probably have this error because in select for the :contact_id the id is missing from the collection.

f.input :contact_id, :as => :select, :collection => Contact.all.collect {|contact| [contact.email, contact.id] }

The above code will set the id for each item in the collection. Check the generated HTML for the attribute value is being correctly set to the id of contact.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.