I have something like this:
class Actor < ActiveRecord::Base
attr_accessible :name, :actor_movie_relationships_attributes
validates :name
has_many :actor_movie_relationships, autosave: true
has_many :movies, through: :actor_movie_relationships
accepts_nested_attributes_for :actor_movie_relationships
end
And:
class ActorMovieRelationship < ActiveRecord::Base
belongs_to :movie
belongs_to :actor
validates :movie, :actor, presence: true
end
Since Actor can have many movies, I'd like to paginate the ActiveAdmin form that is used to administer these relationships.
Currently I have this form (which is NOT paginated):
form do |f|
tabs do
tab 'Actor' do
f.inputs 'Basic Information' do
f.semantic_errors *f.object.errors.keys
f.input :name
f.input :deleted
end
end
tab 'Movies' do
f.inputs 'List of movies' do
f.has_many :actor_movie_relationships, heading: '', new_record: 'Add a Movie', allow_destroy: true do |a|
a.input :Movie, collection: Movie.dropdown_names_map.call
end
end
end
end
f.actions
end
Any idea how to add pagination to Movies in this form?
Related
I would like to ask how to customize active admin.
I`m making my own blog and creating admin page using active admin gem.
This has many to many relationship with article and tag through article_tag table.
What I want to do is to add tag from article panel and I was able to show tag view in article panel, but it is not working fine.(I can`t update or remove tag after save once)
http://localhost:3000/admin/articles/new
image
I made the many to many relation with Article and Tag model like this.
article.rb
class Article < ActiveRecord::Base
has_many :article_tags
has_many :tags, through: :article_tags
accepts_nested_attributes_for :article_tags, :allow_destroy => true
end
tag.rb
class Tag < ActiveRecord::Base
has_many :article_tags
has_many :articles, through: :article_tags
end
article_tag.rb
class ArticleTag < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
And I customized active admin like this.
ActiveAdmin.register Article do
permit_params :title, :description, :url, :image_url, :media, article_tags_attributes: [:article_id, :tag_id, :name, :_destroy, :_edit]
form do |f|
# f.semantic_errors *f.object.errors.keys
f.inputs "Admin Details" do
f.input :title
f.input :description
f.input :url
f.input :image_url
f.input :media
f.input :publish
end
f.inputs "Articles" do
f.has_many :article_tags do |t|
t.input :tag
end
end
f.actions
end
end
But after I saved the article with tag once I can`t update tag or delete tag...
Does anyone know how to fix this?
You forgot to permit :id attribute of article_tag object. It is passed when updating/deleting existing nested object.
ActiveAdmin.register Article do
permit_params :title, :description, :url, :image_url, :media, article_tags_attributes: [:id, :article_id, :tag_id, :name, :_destroy, :_edit]
...
end
When I use ActiveAdmin to edit one Agency, I can select a City and associates it to the Agency. The city is linked to the Agency, but the city is all the times duplicated in the database.
My models:
# agency.rb
class Agency < ActiveRecord::Base
has_many :agency_cities
has_many :cities, through: :agency_cities
accepts_nested_attributes_for :cities, allow_destroy: true
end
# city.rb
class City < ActiveRecord::Base
has_many :agency_cities
has_many :agencies, through: :agency_cities
end
# AgencyCity.rb
class AgencyCity < ActiveRecord::Base
belongs_to :agency
belongs_to :city
end
I read the doc of Activeadmin and added the [:id] permit_parameter, but I still have the problem, I'm very confused.
# admin/agency.rb
ActiveAdmin.register Agency do
permit_params :name, :picture,
cities_attributes: [:id, :name, :description, :_destroy]
form do |f|
f.inputs "Agencies" do
f.input :picture, as: :file
f.input :creation_date, label: 'Creation Date'
f.input :name, label: 'Name'
end
end
f.inputs do
f.has_many :cities do |k|
k.input :name, label: 'City',
as: :select,
collection: City.all.map{ |u| "#{u.name}"}
k.input :_destroy, as: :boolean
end
end
f.actions
end
You are trying to associate the existing cities with an agency, so, you should do it this way:
ActiveAdmin.register Agency do
permit_params city_ids: [] # You need to whitelist the city_ids
form do |f|
f.inputs "Agencies" do
f.input :picture, as: :file
f.input :creation_date, label: 'Creation Date'
f.input :name, label: 'Name'
f.input :cities, as: :check_boxes, checked: City.pluck(&:id) # this will allow you to check the city names that you want to associate with the agency
end
end
end
This will allow you to associate the selected cities to the corresponding agency without creating (duplicating) new cities in the database. I think this is what you are looking for :-)
You can check in the generated html that the option values in the city select input are the name of the city (not the id).
Try this way: collection: City.all.map{ |u| [u.name, u.id]}
Some reference: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
I have a many-to-many relationship set up between blog posts and blog categories using the has_many :through relationship and I am having trouble creating a blog post resource through active admin.
class BlogPost < ActiveRecord::Base
attr_accessible :body, :created_at, :updated_at, :image_url, :title
validates :body, :image_url, :title, presence: true
validates :title, uniqueness: true
has_many :blog_post_categorizations
has_many :blog_categories, :through => :blog_post_categorizations
accepts_nested_attributes_for :blog_categories, allow_destroy: true
end
class BlogCategory < ActiveRecord::Base
attr_accessible :id, :name, :created_at, :updated_at
validates :name, presence: true, uniqueness: true
has_many :blog_post_categorizations
has_many :blog_posts, :through => :blog_post_categorizations
accepts_nested_attributes_for :blog_posts, allow_destroy: true
end
class BlogPostCategorization < ActiveRecord::Base
belongs_to :blog_post
belongs_to :blog_category
end
Now as for my activeadmin formtastic setup, I have:
ActiveAdmin.register BlogPost do
form do |f|
f.inputs "Blog Post" do
f.input :title
f.input :body, as: :html_editor
f.input :image_url
end
f.inputs "Blog Categories" do
f.has_many :blog_categories do |s|
s.input :name
end
end
f.actions
end
end
This allows the new blog post page to load successfully, but when I attempt to submit the new post with categories attached, it results in a "Can't mass-assign protected attributes: blog_categories_attributes" error.
I suspect that my models are set up properly, but the form in ActiveAdmin is not. Any ideas as to how I can correctly set up the form?
Additionally, included below is the blog_category data that's being sent in the params hash of the POST request
"blog_categories_attributes"=>{"1408936652467"=>{"name"=>"cooking"}, "1408936656066"=>{"name"=>"eat"}}
Using Rails version 3.2
Im trying to save a post with category relation.
These are my models
class Post < ActiveRecord::Base
has_many :categorizes
has_many :post_categories, :through=>:categorizes
accepts_nested_attributes_for :post_categories
end
class PostCategory < ActiveRecord::Base
has_many :categorizes
has_many :posts, :through=>:categorizes
end
class Categorize < ActiveRecord::Base
belongs_to :post
belongs_to :post_category
end
and in ActiveAdmin post.rb.
ActiveAdmin.register Post do
permit_params :title, :content, post_category_ids:[:id]
index do
selectable_column
id_column
column :title
column :post_category_id
column :created_at
actions
end
filter :created_at
form do |f|
f.inputs "Post Details" do
f.input :title
f.input :content,:input_html => { :class => "tinymce_editor" }
#f.input :post_categories, :as=> :check_boxes#, :collection => PostCategory.all
end
f.has_many :post_categories ,new_record: false do |c|
c.inputs do
c.input :title
end
end
f.actions
end
controller do
defaults :finder => :find_by_slug_url
end
end
I need to see my all categories from post_categories and i should select more than one.
i check in rails console but the post hasnt any category.
Post.First.post_categories equal to []
Try post_category_attributes instead of post_category_ids
See more here.
I'm trying to get started with active admin. I have this models:
class Client < ActiveRecord::Base
has_many :direcctions
validates :empresa, :presence => true
validates :fono, :presence => true
validates :giro, :presence => true
accepts_nested_attributes_for :direccionts
end
class Direction < ActiveRecord::Base
belongs_to :client
has_one :city
accepts_nested_attributes_for :city
end
class City < ActiveRecord::Base
belongs_to :direction
end
In my Activeadmin.register block for Client I have:
ActiveAdmin.register Cliente do
form do |f|
f.inputs do
f.input :empresa
f.input :fono
f.input :giro
end
f.inputs "Direcciones" do
f.has_many :directions do |j|
j.input :direction
# j.inputs "Ciudad" do
# j.has_one :ciudads do |r|
# r.input :city
# end
# end
end
end
f.buttons
end
end
With this i cant add multiple directions to one cliente, but i can't show the inputs to add a city to a Direction... how can i do that?? and this don't work to.. i have also this error when i try to create a client:
unknown attribute: client_id
Thanks in advance...
ActiveAdmin uses Justin French's Formtastic gem, so you can use that DSL directly in your forms, for example:
f.inputs "Direcciones" do
f.semantic_fields_for :directions do |j|
j.input :direction
j.inputs "Ciudad" do
j.semantic_fields_for :ciudads do |r|
r.input :city
end
end
end
end