HABTM 和 ActiveAdmin 的问题

Problems with HABTM and ActiveAdmin

我已经在 Whosebug 和 Google 上详尽地查找了这个问题,但没有找到可以帮助我解决问题的答案,所以尽管我知道这里几乎没有讨论过这个主题,但我还是要问这个问题。

我在比赛和主题之间有 HABTM 关系,我的日志在发布新比赛或编辑现有比赛时显示了 id 数组,但它根本没有在我的 postgres 数据库中记录任何内容。

我正在使用 ActiveAdmin 1.0 和 Rails 4

下面是我的代码:

class Contest < ActiveRecord::Base
    attr_accessor :subjects_ids

    has_and_belongs_to_many :subjects
    belongs_to :examination_board 
    belongs_to :bureau
    belongs_to :role
    belongs_to :degree
    belongs_to :career
end

class Subject < ActiveRecord::Base
    attr_accessor :contests_ids

    has_many :questions
    has_and_belongs_to_many :contests
end

ActiveAdmin.register Contest do

  permit_params :name,
                :year,
                :state,
                :city,
                :examination_board_id,
                :role_id,
                :degree_id,
                :career_id,
                :bureau_id,
                subjects_ids: [:id]

  # scope :states

  index do
    column "Nome", :name
    column "Banca Examinadora", :examination_board
    column "Matérias" do |subject|
      div :class => 'subjects' do
        subjects = ['yo','la','ho'].join(', ')
      end
    end
    actions
  end

  form do |f|
    f.semantic_errors 
    f.inputs          
    f.input :subjects, as: :check_boxes, collection: Subject.all
    f.actions         
  end

end

我的日志:

Started POST "/admin/contests" for ::1 at 2015-02-27 21:20:05 -0300
Processing by Admin::ContestsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"JYF2JXwaY+6tz++H55QeECpKTjYR+bx0uGG50sIPGWS0AF06inUFPJQtPpp1TlqRPtQmdIZ5B/jAyxfxEgFCtQ==", "contest"=>{"examination_board_id"=>"1", "bureau_id"=>"1", "role_id"=>"1", "degree_id"=>"1", "career_id"=>"1", "name"=>"Concurso Teste 03", "year"=>"2015", "state"=>"São Paulo", "city"=>"São Paulo", "subject_ids"=>["", "1", "3"]}, "commit"=>"Create Contest"}
  AdminUser Load (0.2ms)  SELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."id" =   ORDER BY "admin_users"."id" ASC LIMIT 1  [["id", 1]]
Unpermitted parameter: subject_ids
   (0.2ms)  BEGIN
  SQL (0.3ms)  INSERT INTO "contests" ("name", "year", "state", "city", "examination_board_id", "role_id", "degree_id", "career_id", "bureau_id", "created_at", "updated_at") VALUES (, , , , , , , , , , ) RETURNING "id"  [["name", "Concurso Teste 03"], ["year", 2015], ["state", "São Paulo"], ["city", "São Paulo"], ["examination_board_id", 1], ["role_id", 1], ["degree_id", 1], ["career_id", 1], ["bureau_id", 1], ["created_at", "2015-02-28 00:20:05.163771"], ["updated_at", "2015-02-28 00:20:05.163771"]]
   (5.1ms)  COMMIT
Redirected to http://localhost:3000/admin/contests/3
Completed 302 Found in 50ms (ActiveRecord: 5.8ms)

非常感谢任何建议,谢谢大家!

嗯,事实证明我离成功不远了。我唯一的问题是这一行:

subjects_ids: [:id]

我不应该把那个“:id”加上正确的名字是 "subject_ids"。

就是这样,它现在就像一个魅力。