0

I've looked for this exhaustively on StackOverflow and Google but found no answer to help me out, so I'm asking this though I know the subject has been hardly discussed here.

I have a HABTM relation between Contests and Subjects, my log shows de array of id's when posting a new contest or editing an existing one, but it simply doesn't record anything in my postgres DB.

I'm using ActiveAdmin 1.0 and Rails 4

Below follows my code:

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

My log:

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)

I appreciate any suggestion, thank you all!

dgilperez
  • 10,716
  • 8
  • 68
  • 96
Rob
  • 2,243
  • 4
  • 29
  • 40

1 Answers1

0

Well, it turns out that I was not far from making it. My only problem was this line:

subjects_ids: [:id]

I shouldn't have put that ":id" plus the correct name is "subject_ids".

That's it, it works like a charm now.

Rob
  • 2,243
  • 4
  • 29
  • 40