Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
971 views
in Technique[技术] by (71.8m points)

ruby on rails - How do I permit params for my active admin resource with a has_many_through association?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you tried the accepts_nested_attributes_for function in your Sample model?

class Sample < ApplicationRecord
  ...
  has_many :sample_categories
  has_many :categories, through: :sample_categories

  accepts_nested_attributes_for :categories
end

Try first using test or rails console if it works.

Also please update your admin/sample.rb to allow nested attributes from categories

ActiveAdmin.register Sample do
  permit_params :title, :description, :file_type, :audio_data, :channels, :sample_rate, :file_size,
    categories_attributes: [:id, :name, :etc]
end

Note: if you are using active admin, the samples_controller.rb CRUD controller declaration is not required


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...