Controlling order of fields on active admin edit page - ruby-on-rails

How can I control the order in which fields appear on an active admin edit page? To customize the View page, I've updated admin/model file with:
ActiveAdmin.register Church do
menu :priority => 2
scope :inactive
scope :unregistered
scope :active
scope :registered
show do
attributes_table :name, :address1, :address2, :city, :state, :zip, :office_phone,
:fax, :email1, :active, :registered
end
However, changing "show" to "edit" or "new" results in a no-method error.

Just change the order of the items in your attributes_table and ActiveAdmin will use that order to display.
update: for the edit page...
form do |f|
f.inputs "Label" do
f.input :name
f.input :address1
# etc
end
end

Related

Ruby on Rails and ActiveAdmin: add all object all at once instead add one by one

I'm using Ruby on Rails 5 API app with Active Admin.
I have a model called Subscription with has_many courses (Course is also a model).
When I add courses to a subscription, I need to do it one by one, like in this picture:
What I want is to have an option to add all the courses to a subscription all at once, instead of one by one. I tried using checkbox for multiple selection but it didn't worked. This is the code:
ActiveAdmin.register Subscription do
# require 'lib/app_languages.rb'
permit_params :name, :seat_limit, :domain, :language, :organization, course_ids: [], user_ids: [], subscription_courses_attributes: [:id, :course_id, :_destroy, :_create, :_update]
config.sort_order = 'id_asc'
# Index
index do
id_column
column :name
column :seat_limit
column :domain
column :organization
column :language do |subscription|
AppLanguages.languages[subscription.language]
end
column "Unlocked courses", :courses
column :created_at
actions
end
# Show
show do
attributes_table do
row :name
row :language do |subscription|
AppLanguages.languages[subscription.language]
end
row :seat_limit
row :domain
row :organization
row :created_at
end
panel 'Unlocked Courses' do
table_for subscription.courses do
column :id
column "Title" do |course|
link_to course.title, admin_course_path(course)
end
column "language" do |course|
AppLanguages.languages[course.language]
end
column :author
end
end
panel 'Subscribed users' do
table_for subscription.users do
column :id
column "name" do |user|
link_to user.name, admin_user_path(user)
end
column :email
column "language preference" do |user|
AppLanguages.languages[user.language]
end
end
end
end
# Edit
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs 'Details' do
f.input :name
f.input :language, :as => :select, :collection => AppLanguages.languages_array
f.input :seat_limit
f.input :domain
f.input :organization
end
f.has_many :subscription_courses do |sub_c|
sub_c.inputs "Unlocked Courses" do
if !sub_c.object.nil?
sub_c.input :_destroy, as: :boolean, label: "Destroy?"
end
sub_c.input :course ### should add here option for add all
end
end
f.inputs 'Subscribed users:' do
f.input :users
end
f.actions
end
end
Using for the array the as: :select form input with multi-select should help. Something like this:
sub_c.input :items, as: :select,
collection: courses_collection,
multiple: true
You can try using check_boxes
sub_c.input :items, as: :check_boxes, collection: courses_collection

Active admin Place a form above the table

I have an application using active admin. All I tried to achieve is , to show a form above the active admin table. When the form is submitted , a request is placed which will be displayed in the table below the form. What I tried is,
Tried to render the form in a panel within index
Tried to have 2 index, one with the table and the other with rendering the form
The problem I faced in the above two solutions is , when there are no records in the table , the form is also not shown . When the table has records, the form is displayed
Is there a way so that the form is shown even if there are no records in index table. or, other ways of showing the form above the table. I also tried to write custom index, but could not locate the exact documentation for that
First off, to render a form you normally do:
form html: {:multipart => true} do |f|
f.inputs do
f.input :name
f.input :content
f.input :description
f.input :price
f.input :plan
f.input :image, hint: f.project.image? ? image_tag(project.image.url, height: '100') : content_tag(:span, "Upload JPG/PNG/GIF image")
end
f.actions
end
inside the model. In my case it was projects.rb for project.rb
If you want to render a form via partial, simply do:
# renders app/views/admin/posts/_some_partial.html.erb
render 'some_partial', { post: post }
this will render any form into your admin panel.
If you want to insert an index:
ActiveAdmin.register Task do
permit_params :title, :note, :video, :header, :tag, :project_id
sortable tree: false, sorting_attribute: :tag
index :as => :sortable do
label :title
actions
end
index do
selectable_column
column :header
column :title
column :tag
column :project
actions
end
end

Active Admin remove certain fields while going for updation

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

Blank ActiveAdmin pages

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

Nested form in activeadmin not saving updates

I have a nested form in ActiveAdmin for these models (a :class_section has_many :class_dates):
class ClassDate < ActiveRecord::Base
belongs_to :class_section
validates :start_time, :presence => true
validates :end_time, :presence => true
end
and
class ClassSection < ActiveRecord::Base
belongs_to :class_course
has_many :class_dates
belongs_to :location
accepts_nested_attributes_for :class_dates
end
Everything seems to be in the right place when I look at the form. However, when I update a class_date, it doesn't save.
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id
form do |f|
f.inputs "Details" do
f.input :class_course, member_label: :id_num
f.input :min_students, label: "Minunum Students"
f.input :max_students, label: "Maxiumum Students"
f.input :location
end
f.inputs do
f.input :info, label: "Additional Information"
end
f.inputs "Dates" do
f.has_many :class_dates, heading: false do |cd|
cd.input :start_time, :as => :datetime_picker
cd.input :end_time, :as => :datetime_picker
end
end
f.actions
end
index do
column :class_course
column :location
default_actions
end
filter :class_course
filter :location
show do |cs|
attributes_table do
row :class_course do
cs.class_course.id_num + " - " + cs.class_course.name
end
row :location
row :min_students, label: "Minunum Students"
row :max_students, label: "Maxiumum Students"
row :info, label: "Additional Information"
end
panel "Dates" do
attributes_table_for class_section.class_dates do
rows :start_time, :end_time
end
end
active_admin_comments
end
end
Here is the ActiveAdmin file for ClassDates:
ActiveAdmin.register ClassDate, as: "Dates" do
permit_params :start_time, :end_time, :class_section_id
belongs_to :class_section
end
Can you see a reason why it's not saving properly?
UPDATE: I added the following code to the AA and it seems to work now:
controller do
def permitted_params
params.permit!
end
end
Let me know if there is a better solution. Thanks.
UPDATE 2: There is one lingering problem however. I am unable to delete ClassDates using this form.
You need to permit nested parameters, but you should never use params.permit!. It's extremely unsafe. Try this:
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id,
class_dates_attributes: [ :id, :start_time, :end_time, :_destroy ]
form do |f|
# ...
f.inputs "Dates" do
f.has_many :class_dates, heading: false, allow_destroy: true do |cd|
cd.input :start_time, :as => :datetime_picker
cd.input :end_time, :as => :datetime_picker
end
end
f.actions
end
# ...
end
The configuration (and permitted_params) of your ClassDate admin panel has nothing to do with the permitted parameters within the ClassSection admin panel. Treat them as separate controllers within the app.
Adding the allow_destroy: true option to the has_many call will add a checkbox to the form to allow you to delete a class time upon form submission.

Resources