activeadmin has_one select from collection to update related foregn_key - ruby-on-rails

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.

Related

ActiveAdmin: Add association on object creation

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.

How to set permit_params for another model

These are my models:
Product.rb:
class Product < ApplicationRecord
belongs_to :position
has_many :products_sizes
has_many :sizes, through: :products_sizes
has_many :reviews, dependent: :destroy
accepts_nested_attributes_for :products_sizes
end
Products_size.rb:
class ProductsSize < ApplicationRecord
belongs_to :product
belongs_to :size
has_many :prices
accepts_nested_attributes_for :prices
end
Size.rb:
class Size < ApplicationRecord
has_many :products_sizes
has_many :products, through: :products_sizes
end
and Price.rb:
class Price < ApplicationRecord
belongs_to :products_size
end
In ActiveAdmin I need to make a form for Product, for when I update the product, I could create a Price, so a part of the form looks like this:
... #here is the begining of the form
f.inputs 'Sizes' do
f.semantic_fields_for ProductsSize.where(product_id: params[:id], size_id: Product.find(params[:id]).products_sizes.size.to_i).first.prices.new do |ps|
ps.input :products_size_id, label: 'Size', as: :select, collection: Product.find(params[:id]).sizes.map { |s| ["#{s.title}", s.id] }
ps.input :quantity
ps.input :amount
li do
link_to 'Add size', '#'
end
end
end
It's all seems to be good, except when clicking the submit button, the Price isn't created. I think, that's because the permit_params are not specified for price. How can I specify them? Thanks.
ActiveAdmin.register Post do
permit_params :title,
comments_attributes: [:name, :hidden]
end
Post is a moodel and comments is another. You can use params from comments with comments_attributes, if your model name is price you can use price_attributes: [...params...].
Here is how you allow parameters in active admin
ActiveAdmin.register Post do
permit_params :title, :content, :author
end
This is just an example, use your own params

ActiveAdmin Form Multiple Nested HMT relationships with has_many

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

Rails 4 has many through with active admin

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: []

has_many through form not registering in the join table

I have a problem with my ruby on rails 4 app with the activeadmin gem
I have generated multiple resources that works well but I have a problem with a resource that has a has_many through relationship. The new records are not registered through the join table.
Here is my model :
video.rb
class Video < ActiveRecord::Base
has_many :album_videos
has_many :albums, :through => :album_videos
end
Album.rb
class Album < ActiveRecord::Base
has_many :album_videos
has_many :videos, :through => :album_videos
end
Album_video.rb
class AlbumVideo < ActiveRecord::Base
belongs_to :video
belongs_to :album
end
And the activeadmin file for album
album.rb
ActiveAdmin.register Album do
menu :priority => 6, :label => proc{ "Album de vidéos" }
filter :album_videos
permit_params :titre, :description, videos_attributes: [:id, :titre, :_update,:_create]
form do |f|
f.inputs
f.inputs "Vidéos" do
f.input :videos, as: :check_boxes, collection: Video.all, :member_label => :titre
end
f.actions
end
end
Album's form is well displayed, I have a panel with all my registered videos.
When I create a new album, the album is registered but nothing in the album_videos table.
Anything I am missing ?
I found the solution. You juste have to add
video_ids: []
to your permit_params.
Mine looks like this now :
permit_params :titre, :description, video_ids: []

Resources