Question

I have a Campaign model and a Category model. They have a has-many-through relationship with each other. The intermediate model is campaign_categories. Campaign:

class Campaign < ActiveRecord::Base
  attr_accessible :name, :carousel_image, :show_in_carousel, :title, :carousel_description,
                  :video_embed_code, :goal, :end_date, :backer_count, :author_name, :author_photo,
                  :author_description_md, :author_description_html, :description_markdown, :description_html,
                  :funds_description_md, :funds_description_html, :campaign_status_id


  #associations
  has_many :campaign_categories
  has_many :categories, through: :campaign_categories

end

Category:

class Category < ActiveRecord::Base
  attr_accessible :name

  #associations
  has_many :campaign_categories
  has_many :campaigns, through: :campaign_categories
end

Campaign_Category:

class CampaignCategory < ActiveRecord::Base
  attr_accessible :campaign_id, :category_id
  belongs_to :campaign
  belongs_to :category
end

I have following in campaigns.rb for Activeadmin:

ActiveAdmin.register Campaign do

  form :html => { :enctype => 'multipart/form-data'} do |f|
    f.inputs "Campaign Basic Information" do
      f.input :name
      f.input :categories
    end
    f.actions
  end
end

The categories show up correctly in a multi select box. But I receive following error on form submission: Can't mass-assign protected attributes: category_ids

I tried calling accepts_nested_attributes_for :categories in Campaign, but that did not work. How can I resolve this issue? Thanks.

Était-ce utile?

La solution

Adding :category_ids to your attr_accessible call in the Campaign model should do it

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top