Active admin Place a form above the table - ruby-on-rails

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

Related

Activeadmin form use show view

How can i implement such thing as display part of my view page on edit page
I want to show some data under my edit form.
form do |f|
f.semantic_errors(*f.object.errors.keys)
f.inputs 'Edit SSR' do
f.input :scope
f.input :code
f.input :name
end
f.actions
and here my view data goes
.....
end
I have tried to override form but no luck
How i can do it?
If you want to show data below the form then you can write it inside div as follows in the active_admin resource file:
form do |f|
f.semantic_errors(*f.object.errors.keys)
f.inputs 'Edit SSR' do
f.input :scope
f.input :code
f.input :name
end
f.actions
div id: "div_id", class: "div_class" do
#put your data here
end
end
OR you can create a partial and call it as:
form partial: "partial_name"

Rails: Any way to generate Active Admin views (add nested forms)?

Is there anyway to generate Active Admin's views? I know how to override them but I'd like to keep their basic layout but just add some nested forms.
According to Active Admin Documentation, there is no way to generate all views:
You can create forms with nested models using the has_many method:
ActiveAdmin.register Post do
form do |f|
f.inputs "Details" do
f.input :title
f.input :published_at, :label => "Publish Post At"
end
f.inputs "Content" do
f.input :body
end
f.inputs do
f.has_many :categories, :allow_destroy => true, :heading => 'Themes', :new_record => false do |cf|
cf.input :title
end
end
f.actions
end
end
The :allow_destroy option will add a checkbox to the end of the nested form allowing removal of the child object upon submission. Be sure to set :allow_destroy => true on the association to use this option.
The :heading option will add a custom heading to has_many form. You can hide a heading by setting :heading => false.
The :new_record option will show or hide new record link at the bottom of has_many form. It is set as true by default.

Ruby On Rails Active Admin has_many changing text to use a different column

This is similar to my previous question Ruby On Rails Active Admin has_many changing dropdown to use a different column
I figured out how to reassign a f.inputs but how would i go about reassigning the display of the data when viewing an item...
e.g:
Public Git Repo: https://github.com/gorelative/TestApp
Snippet of code i have in my fillups.rb
ActiveAdmin.register Fillup do
form do |f|
f.inputs do
f.input :car, :collection => Car.all.map{ |car| [car.description, car.id] }
f.input :comment
f.input :cost
f.input :mileage
f.input :gallons
f.buttons
end
end
end
Modify the show action
ActiveAdmin.register Fillup do
# ... other stuff
show do
attributes_table do
# add your other rows
row :id
row :car do |fillup|
link_to fillup.car.description, admin_car_path(fillup.car)
end
end
end
end

Embed form (has many) with activeadmin

I am making a blog using rails and activeadmin. I associate tags to each post. I want to know how to configure activeadmin to add tags on posts.
I want to be able to create tags from a new Post form. How can i do that?
Thank you
if Posts has_many Tags then something like this could work.
form do |f|
f.inputs do
f.input :title
f.input :content
f.has_many :tags do |fu|
fu.input :name
fu.input :_destroy, :as=>:boolean, :required => false, :label => 'Delete Tag' unless fu.object.new_record?
end
end
end
ActiveAdmin expects that the Post model accepts_nested_attributes_for :tags
like..
class Post
#....
accepts_nested_attributes_for :tags
end

Controlling order of fields on active admin edit page

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

Resources