I'm having trouble setting my permitted params in active admin. The docs say "Any form field that sends multiple values (such as a HABTM association, or an array attribute) needs to pass an empty array to permit_params". Here is my code from admin/sample.rb:
ActiveAdmin.register Sample do
permit_params :title, :description, :file_type, :audio_data, :channels, :sample_rate, :file_size,
categories: []
end
When I try this all the attributes appear in my active admin table but there is nothing for categories
. I'm not really sure where I'm going wrong. I've tried category_ids
and sample_categories
too but still it doesn't show in the table. I've also tried adding an attribute to the array, such as categories: [:name]
but still nothing.
Everything behaves as it should in the app itself and I'm able to add a category when creating a sample
, for example. Any suggestions about how to solve this? Here is some relevant code.
models/sample.rb
class Sample < ApplicationRecord
...
has_many :sample_categories
has_many :categories, through: :sample_categories
end
models/category.rb
class Category < ApplicationRecord
has_many :sample_categories
has_many :samples, through: :sample_categories
end
models/sample_category.rb
class SampleCategory < ApplicationRecord
belongs_to :sample
belongs_to :category
end
controllers/samples_controller.rb
...
def sample_params
params.require(:sample).permit(:title, :description, :audio, :file_type, :file_size, :sample_rate, :channels, :tag_list, category_ids: [])
end
...
question from:
https://stackoverflow.com/questions/65947050/how-do-i-permit-params-for-my-active-admin-resource-with-a-has-many-through-asso 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…