ActiveModel::UnknownAttributeError 问题

问题描述 投票:0回答:1

我目前正在使用 ActiveAdmin 开发 Rails 应用程序,并设置了两个模型:UniversityLocations 和 Programs,以及它们的关联。

这是我迄今为止实现的代码:

module Uploading
  class UniversityLocations < Uploading::ApplicationRecord
    self.table_name = :university_locations
    belongs_to :program, class_name: 'Uploading::Programs', foreign_key: 'program_id'
 end
end

module Uploading
  class Programs < Uploading::ApplicationRecord
    self.table_name = :programs
    has_many :university_locations, class_name: 'Uploading::UniversityLocations', dependent: :destroy
    accepts_nested_attributes_for :university_locations, allow_destroy: true
    end
    end

迁移

class CreateLocations < ActiveRecord::Migration[6.0]
  def change
    create_table :university_locations do |t|
      t.text :location
      t.references :program, null: false, foreign_key: { to_table: :programs }
    end
  end
end

架构

create_table "university_locations", force: :cascade do |t|
    t.text "location"
    t.bigint "program_id", null: false
    t.index ["program_id"], name: "index_university_locations_on_program_id"
  end

我使用主动管理作为框架

ActiveAdmin.register Uploading::Programs, as: 'Institutions' do
  menu priority: 1
  menu parent: "Programs & Institutions"

  permit_params :university_name, :ranking, :institution_type, :sponsor_license_number,
                :offer_tat, :application_fees, :post_study_work_permit,
                :conditional_offer_letter, :additional_services, :overview, 
                :workflow, :eligibility, interview_prep_material: [], images: [], university_locations_attributes: [:id, :location, :_destroy]
          ......
          ......
          ......
          ......
          ......
         form do |f|
    f.inputs do
          ......
          ......
          ......
          ......
          ......
      f.has_many :university_locations, allow_destroy: true, new_record: true, heading: 'Locations' do |location_form|
        location_form.input :location, as: :quill_editor, input_html: quill_options
      end
    end
    f.actions
  end

遇到的问题 当我尝试创建新程序时,收到以下错误:

Started POST "/admin/institutions" for 127.0.0.1 at 2024-10-28 13:14:59 +0000

Processing by Admin::InstitutionsController#create as HTML

  Parameters: {"programs"=>{"university_locations_attributes"=>{"0"=>{"location"=>"<p>LA</p>"}}}, "commit"=>"Create Programs"}


Completed 500 Internal Server Error in 117ms (ActiveRecord: 40.9ms | Allocations: 36806)  

ActiveModel::UnknownAttributeError (unknown attribute 'programs_id' for Uploading::UniversityLocations.):

我设置模型关联的方式是否有问题? 该问题是否与外键的命名有关? ActiveAdmin 表单设置中是否存在我应该解决的缺失参数?

ruby-on-rails ruby activerecord activeadmin
1个回答
0
投票

正如评论中提到的,我意识到我忽略了类名约定并已修复该问题..

© www.soinside.com 2019 - 2024. All rights reserved.