Creating a Rails 3 HABTM with Active Admin throws a 'Can't mass-assign protected attributes:' error
Asked Answered
H

1

5

I am a rails noob so the below is probably down to lack of understanding however I have been looking/reading all day and cannot seem to find the solution.

I have two models - project and technology :

Project :

class Project < ActiveRecord::Base

  attr_accessible description, :name

  has_and_belongs_to_many :technologies, :join_table => :projects_technologies

end

Technology :

class Technology < ActiveRecord::Base

  attr_accessible :abbr, :description, :name

  has_and_belongs_to_many :projects, :join_table => :projects_technologies

end

My Create_Projects_Technologies migration was as follows :

class CreateProjectsTechnologies < ActiveRecord::Migration
  def self.up

    create_table :projects_technologies, :id => false do |t|
        t.references :project
        t.references :technology
    end
    add_index :projects_technologies, [:project_id, :technology_id]
    add_index :projects_technologies, [:technology_id, :project_id]
  end

  def self.down
    drop_table :projects_technologies
  end
end

I am then using Active Admin to create and edit Project models using the following form :

ActiveAdmin.register Project do

  form do |f|
    f.inputs "Project attributes" do
      f.input :name
      f.input :description
      f.input :technologies, as: :check_boxes
    end
    f.buttons
  end

end

This correctly shows all my technologies as check boxes however as soon as I submit the form I hit the following error which I have not been able to overcome :

ActiveModel::MassAssignmentSecurity::Error in Admin::ProjectsController#update

Can't mass-assign protected attributes: technology_ids

All help is very much appreciate :D

Hairdo answered 12/8, 2012 at 19:17 Comment(1)
In case anyone else gets this error mine came up Can't mass-assign protected attributes: #{attrs.join} rather than having the category_ids.Swats
O
7

Simple add technology_ids to Project attr_accessible

attr_accessible :client, :description, :name, :technology_ids
Osteotomy answered 12/8, 2012 at 19:37 Comment(2)
Thank you so much! I can't believe I missed that. Something about not seeing the forest because of all the trees :PHairdo
but what about mass assignment? i mean, i understand that because of this is an admin area nobody without special rights can do anything but still this doesn't look right. (i'm talking about adding technology_ids to attr_accessible)Ro

© 2022 - 2024 — McMap. All rights reserved.