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.
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 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: []
Actually i'm on a project for a model many->many. I need to find with a current user all credits/devices/project (and I think it's useless to have a table credit with only two column (id & score) so I merge this table to the join table).
I get this error :
SystemStackError in Users#show
Showing app/views/shared/_credit.html.erb where line # raised:
stack level too deep
And the two model :
class **Credit** < ActiveRecord::Base
attr_accessible :created_at, :credit_id, :device_id, :project_id, :score, :user_id
belongs_to :device
belongs_to :user
belongs_to :project
belongs_to :score
end
class **User** < ActiveRecord::Base
has_many :credit
has_many :credit, :through => :credit, foreign_key: "user_id", dependent: :destroy
end
Thank !
Best.
Stack level to deep points to an infinitive recursive call, and I would say you get that with
has_many :credit, :through => :credit,
which clearly introduces a cycle of some sort.