I am trying to set up a standard relationship as following:
class Category < ActiveRecord::Base
has_many :post_categories
has_many :posts, :through => :post_categories
accepts_nested_attributes_for :post_categories
end
class Post < ActiveRecord::Base
has_many :post_categories
has_many :categories, :through => :post_categories
accepts_nested_attributes_for :post_categories
attr_accessor :category_ids
end
class PostCategory < ActiveRecord::Base
belongs_to post
belongs_to category
end
I am using ActiveAdmin and need to set up checkboxes to describe the relationship.
I have tried many different ways to have the checkboxes save. Here is my admin post.rb file:
ActiveAdmin.register Post do
permit_params :content, category_ids: []
form do |f|
f.inputs # Include the default inputs
f.inputs "Categories" do
f.input :categories, as: :check_boxes, collection: Category.all
end
f.actions # Include the default actions
end
end
I have tried different permit params such as
permit_params :content, :categories
permit_params :content, post_categories_attributes: [:id, :post_id, :category_id]
permit_params :content, category_ids: [:id]
The database is set up as shown in the rails tutorial, and the relationship seems to work elsewhere except for being saved from activeadmin. I even tried to use param.permit! to permit all params, but still no luck.
I have found many posts of seemingly the same question, but many give different answers and nothing seems to work.
What is wrong?
Little late but this question is googled first for me, so I assume it can be helpful to other "googlers"
#models/post.rb
accepts_nested_attributes_for :categories#, allow_destroy: true
#AA Post conf file
permit_params :content, category_ids: []
Related
When creating a new object and connecting it with existing (has_many :through) resources I need to:
Save the new object first
Edit this newly created object again to add connections to the nested resources.
Cumbersome! It seems ActiveAdmin tries to create the association first, then the main object. Is it somehow possible to do this object creation + associating nested resources in one go?
Is case a more concrete example is needed, here is an example of my data model and ActiveAdmin setup:
Person < ActiveRecord::Base
has_many :organizations, through: :person_organizations
end
Organization < ActiveRecord::Base
has_many :people, through: :person_organizations
end
PersonOrganization < ActiveRecord::Base
belongs_to :person
belongs_to :organization
validates :person, presence: true
validates :organization, presence: true
end
form do |f|
f.inputs do
f.input :name
end
f.inputs 'Organizations' do
f.has_many :person_organizations, do |connection_f|
connection_f.input :organization, as: :select,
collection: Organization.select[:id, :name],
member_label: proc { |org| org.name }
end
end
end
You have to add
accepts_nested_attributes_for :organizations, allow_destroy: true
and if you haven't also the
has_many :person_organizations
in your Person model, and you can place
f.input :organizations, :multiple => true
in your form. Also make sure you permit the correct params in your activadmin register block. In this case it would be
permit_params :name, :organization_ids => []
Read carefully: https://activeadmin.info/5-forms.html#nested-resources and https://activeadmin.info/2-resource-customization.html#setting-up-strong-parameters
I like to decorate multiple select inputs with the select2 javascript library. Let me know if something does not work out.
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
...
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
Can't find a solution a week along, please help
Here I have 2 models
Item.rb
class Item < ActiveRecord::Base
has_one :specs_group, class_name: Spec, :dependent => :nullify
attr_accessible :specs_group_attributes
accepts_nested_attributes_for :specs_group, update_only: true
end
Spec.rb
class Spec < ActiveRecord::Base
belongs_to :item
attr_accessible :item_id
end
Here is admin/item.rb
ActiveAdmin.register Item do
form do |f|
f.inputs "Specs group" do
f.has_many :specs_group do |s|
s.input :title, as: :select, collection: Spec.roots.map { |a| [a.title] }
end
end
f.actions
end
end
I want to choose element from Spec collection and update corresponding Spec item. With code above I can choose from dropdown list, but it updates Spec's title or creates new. It's a Rails 3.2 app.
i'm new to ActiveAdmin and Rails and i struggle on something to build up my ActiveAdmin interface.
Consider the following models :
class PageType < ActiveRecord::Base
has_many :fields, class_name: 'PageField'
accepts_nested_attributes_for :fields, allow_destroy: true
end
class PageField < ActiveRecord::Base
belongs_to :page_type
has_many :page_has_fields
has_many :pages, through: :page_has_fields
accepts_nested_attributes_for :page_has_fields, allow_destroy: true
end
class PageHasField < ActiveRecord::Base
belongs_to :page
belongs_to :page_field
end
class Page < ActiveRecord::Base
belongs_to :page_type
has_many :page_has_fields, dependent: :delete_all
has_many :page_fields, through: :page_has_fields
accepts_nested_attributes_for :page_fields, allow_destroy: true
end
In Active Admin I want to create some page templates to handle "static" pages. And in each of the pages, I want to update the content of each fields related to the templates page.
Thus far, what I did worked with this code :
ActiveAdmin.register Page do
permit_params :name, :page_type_id, :page_id,
:page_fields_attributes => [:id, :name, :field_type, :page_id,
:page_has_fields_attributes => [:id, :content, :page_id]
]
form do |f|
f.inputs
f.has_many :page_fields, heading: false, new_record: false do |g|
g.inputs :name, :required
g.has_many :page_has_fields, new_record: false do |h|
h.input :content if h.object.page_id == f.object.id
end
end
f.actions
end
end
But the second has_many seems really wrong to me, and i'm sure there are a better solution to this problem.
If i don't go with the "if", inputs are created for the right fields, but for every single page.
Is there a way to specify an ID or a parameter in has_many ? Or a better tag to handle situation like this ?
Thanks
Try changing your setup to something more like this
ActiveAdmin.register Page do
...
form do |f|
f.inputs do
f.input :some_column
f.input :some_other_column
f.input :page_fields, as: :check_boxes, checked: PageField.all.map(&:name)
f.input :page_has_fields, as: :check_boxes, checked: PageField.all.map(&:content)
end
f.actions
end
end
My new comments model works well on the website, but there is an issue with activeadmin because when I go to my admin view and try to view 'guidelines' (another model), I get an error message:
undefined method `comment' for
My model comment.rb:
belongs_to :guideline
belongs_to :commenter, class_name: 'User'
attr_accessible :body, :commenter_id
My model guideline.rb:
attr_accessible :content, :hospital, :title, :user_id, :guideline_id, :specialty, :updated_by, :current_user, :subtitle, :slug, :activities, :comment, :visible
belongs_to :user
has_many :favourite_guidelines
has_many :comments, :dependent => :destroy
admin/guidelines.rb:
index do
column :comment
default_actions
end
You get an undefined method error because your guideline model has_many comments and therefore has the method .comments but not .comment. If you are trying to show the number of comments a guideline has then you can do this.
column "Comments" do |guideline|
guideline.comments.count
end
If you want to show all the actual comments listed out you can collect whatever the column is in the comment object that holds the text and join them with a comma or newline etc.
column "Comments" do |guideline|
guideline.comments.collect(&:text_form_of_comment).join(",")
end
HERE is more info on how you can customize you ActiveAdmin index tables.