HABTM和ActiveAdmin的问题

时间:2015-02-28 00:24:25

标签: ruby-on-rails postgresql activeadmin has-and-belongs-to-many

我已经在StackOverflow和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" = $1  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 ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) 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)

我感谢任何建议,谢谢大家!

1 个答案:

答案 0 :(得分:0)

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

subjects_ids:[:id]

我不应该把那个&#34;:id&#34;加上正确的名称是&#34; subject_ids&#34;。

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