My ActiveAdmin index page for ad_images works fine but new, edit, and view look blank like this...
admin/ad_image.rb looks like this:
ActiveAdmin.register AdImage do
permit_params :name, :url, :imageable_type, :imageable_id
form(:html => { :multipart => true }) do |f|
f.inputs "Ad Image" do
f.input :name
f.input :url, as: :file
f.input :imageable_type
f.input :imageable_id
end
f.actions
end
end
The AdImage model works normally otherwise. Any clue what's going on?
UPDATE
Turns out that "ad_image" conflicts with a formtastic style of the same name... I think? When I remove the "ad_image" class from the form (which is added automatically) everything works fine.
I can give an alias to my ActiveAdmin class like so
ActiveAdmin.register AdImage, as: "ImagesForAds"
... and the form will appear. But now I can't save anything...
Edit admin/ad_image.rb :---
ActiveAdmin.register AdImage do
permit_params :name, :url, :imageable_type, :imageable_id
form do |f|
f.inputs "Ad Image" do
f.input :name
f.input :url, as: :file
f.input :imageable_type
f.input :imageable_id
end
f.actions
end
end
Related
ActiveAdmin automatically pulls up the persisted information for an object when the update form is displayed. It doesn't show the images though - doesn't even show the name in my form - how do I fix this to show the actual image and it's name?
ActiveAdmin.register Art do
permit_params :art_pic_attachments_attributes: [:id, :picture, :_destroy]
form(html: { multipart: true }) do |f|
f.inputs do
f.has_many :art_pic_attachments, allow_destroy: true, heading: 'Images' do |ff|
ff.file_field :picture, required: true, heading: "Pictures"
end
end
f.actions
end
Maybe this wiki article will help. The key is to use an image tag as the input hint.
f.inputs "Attachment", :multipart => true do
f.input :cover_page, :as => :file, :hint => image_tag(f.object.cover_page.url)
f.input :cover_page_cache, :as => :hidden
end
In my application I have 2 models: AdminUser, which has_many :announcements, and Announcement, which belongs_to :admin_user. The Announcement table has admin_user_id column in a database.
In app/admin/announcement.rb I have:
ActiveAdmin.register Announcement do
permit_params :title, :body, :admin_user_id
controller do
def new
#announcement = Announcement.new
#announcement.admin_user_id = current_admin_user.id
end
end
form do |f|
f.inputs do
f.input :title
f.input :body, as: :ckeditor
end
f.actions
end
end
Why my controller doesn't work? When I create an announcement through activeadmin, the column admin_user_id is empty. How can I solve this issue?
Just added to a form: f.input :admin_user_id, as: :hidden, and now everything is working.
Hi i have an admin page using active admin gem.
thing is while creating a new page i should be able to input name, amount and interval..But while updating only name field must show..other 2 values shouldnt be updated. This is my active admin file. How to make this happen. Thanks in advance
ActiveAdmin.register SubscriptionPlan do
menu priority: 10
permit_params :name, :amount, :interval
index do
selectable_column
default_actions
column :name
column :amount
column :interval
end
form do |f|
f.inputs "Subscription Plan" do
f.input :name
f.input :amount
f.input :interval, as: :select, collection:["week","month","year"]
end
f.actions
end
end
Try this
form do |f|
f.inputs "Subscription Plan" do
f.input :name
if f.object.new_record?
f.input :amount
f.input :interval, as: :select, collection:["week","month","year"]
end
end
f.actions
end
I looked at this question and was able to get set up a has_many form properly.
Here's where I'm at:
f.has_many :related_contents do |rc|
rc.inputs "first configuration" do
#...
end
end
f.has_many :related_contents do |rc|
rc.inputs "second configuration do
#...
end
end
so I want to have different configurations of fields. What I can't figure out is how to set the <h3> that activeadmin generates and sets as the title for the nested fields. I thought something like
f.has_many :related_contents, :title => "first set" do |rc|
would work, but It does not. Does anybody know what I need to do to get this working right?
I assume that changing the <h3> will also change the button that gets generated to add. Is there another option I need to set for that as well?
Add a :heading option to the f.has_many method
f.has_many :related_contents, :heading => "first set" do |rc|
#...
end
To remove the heading altogether, set it to false or nil
$('h3:contains("Related Contents")').hide().first().show();
Not perfect, but it definitely looks better.
you need to follow the next order, i mean, you need to set on first line the "inputs" and then the "has_many" block, I hope this is what you are looking, regards!
form do |f|
f.inputs "details #1" do
f.input :title, :label => "Title"
f.input :description, :label => "Description"
f.input :image, :as => :file, :required => false, :label=>'Image'
end
f.inputs "details #2" do
f.has_many :subdetails do |a|
a.input :description, :label => "Description"
a.input :image, :as => :file, :required => false, :label=>'Image'
end
end
Ok, after have some problems with the title of has_many I could figure out how is the correct way for do it and i think is more cleaner and fancy...
form do |f|
f.inputs "Post" do
f.input :title
f.input :description
end
f.inputs "Comments" do
f.has_many :comments do |a|
a.input :title
a.input :description
end
end
f.buttons
end
I have a infrastructure object composed for many datacenters. In the apps/admin/infrastructures.rb I have the following code:
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
I can add datacenters with no problems but I don't know how I can delete it from infrastructure form.
Sep 2017 Update:
Rails 5.1.4, ActiveAdmin 1.0.0
Append :id and _destroy in permit_params along with other attributes from the model e.g. :name in your case. Then provide the :allow_destroy option in f.has_many too. Other requirements remain the same; like adding allow_destroy: true in accepts_nested_attributes_for.
Final look:
ActiveAdmin.register Infrastructure do
permit_params :name, datacenters_attributes: [:id, :_destroy, :name]
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters, heading: false,
allow_destroy: true,
new_record: false do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
end
ActiveAdmin Reference
This worked for me:
i.input :_destroy, as: :boolean
and in the Model remember to add :allow_destroy :
accepts_nested_attributes_for :images, allow_destroy: true
Solved adding the following line:
datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
The code looks like:
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.input :name
datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
end
end
f.buttons
end
If you cant destroy the object nested. You need to put :_destroy in your app/admin/object.rb permit_params
permit_params :id,:name, :cod, :_destroy
I hope this will be helpful (I've changed my code to suit your example, so I hope there are no typos here):
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.inputs :datacenters do
datacenter_form.input :name
end
datacenter_form.buttons do
link_to "Delete", admin_datacenter_path(datacenter_form.object), method: "delete", class: "button" unless datacenter_form.object.new_record?
end
end
end
f.buttons
end
and the controller method should be defined in datacenters.rb
controller do
def destroy
#datacenter = Datacenter.find(params[:id])
#datacenter.destroy
redirect_to edit_admin_retailer_path(#datacenter.infrastructure)
end
end
This should work:
datacenter_form.label :_delete
datacenter_form.check_box :_delete
This adds a checkbox for each nested object which will delete the object if checked.
Don't forget to add the following to your parent model
has_many :child_name, :dependent => :destroy