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.
Related
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
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: []
I have 3 models:
class Brand
attr_accessible :obs, :site, :title
has_many :equipments
end
class Equipment
attr_accessible :brand_id, :category_id, :lending_id
belongs_to :brand
has_many :lendings
end
class Lending
attr_accessible :equipment_id
belongs_to :equipment
end
I'm trying to show the brand of an associated equipament:
Brand: <%= #lending.equipment.brand %>
that command show this: Brand:0xab7f2c8
As you can see, there's no association between brand and lending models and for me its strange if i do that. I want to use the equipment/brand association to retrieve the :title information and show it on my lending view.
Can anyone help me?
You can either use a delegate in Lending:
delegate :brand, :to => :equipment, allow_nil: true
Or you can setup a has-one-through association in Lending:
has_one :branch, :through => :equipment
Either way, you can now call branch directly from a Lending instance, and work on it (almost) as if it were a regular association.
Use delegate
class Lending
attr_accessible :equipment_id
belongs_to :equipment
delegate :brand, :to => :equipment, :allow_nil => true
end
now you can use
<%= #lending.brand.title%>
I'm fighting with this bug for the past few hours and I can't make sense of it and my researches didn't give an answer.
It is a basic HABTM relationship. Inputs HABTM Visualizations, and I have a cross table InputsVisualizations that has some attributes of its own.
= form_for(#visualization) do |f|
= f.input :title
= f.fields_for :inputs_visualizations do |iv|
= iv.input :color
= iv.fields_for :input do |i|
= i.input :title
= f.button :submit, "Save"
class Input < ActiveRecord::Base
# Associations ------------------
has_many :inputs_visualizations, dependent: :destroy, order: "inputs_visualizations.order ASC"
has_many :visualizations, through: :inputs_visualizations
# Attributes --------------------
attr_accessible :title, :unit
end
class InputsVisualization < ActiveRecord::Base
# Associations ------------------
belongs_to :input
belongs_to :visualization
# Attributes --------------------
attr_accessible :input_id, :visualization_id, :color, :input_attributes
accepts_nested_attributes_for :input, :reject_if => lambda { |i| i[:title].blank? }, :allow_destroy => true
end
class Visualization < ActiveRecord::Base
# Associations ------------------
has_many :inputs_visualizations, dependent: :destroy, order: "inputs_visualizations.order ASC"
has_many :inputs, through: :inputs_visualizations, order: "inputs_visualizations.order ASC"
# Attributes --------------------
attr_accessible :title, :inputs_visualizations_attributes
accepts_nested_attributes_for :inputs_visualizations, :reject_if => lambda { |a| a[:input_id].blank? }, :allow_destroy => true
end
I need a form for Visualizations that let me manage both InputsVisualizations and Inputs. As you can see in my form, there are two nested fields_for.
Case 1:
I create a nested InputsVisualization with a nested Input (both are new_record). I save the form, they both are created. Cool!
Case 2:
From the same form, I update an Input (existing record). I save, nothing is updated even though the attributes are properly passed to the controller.
I read that nested_attributes don't work with belongs_to relationship, though it created it just fine. Why doesn't it update afterwards?
Thanks
The :reject_if condition on this line looks for an :input_id but that value is not included in the form. So this could prevent the update from going through.
accepts_nested_attributes_for :inputs_visualizations, :reject_if => lambda { |a| a[:input_id].blank? }, :allow_destroy => true