active admin / formtastic , add all inputs in a single block - ruby-on-rails

A report, has_many answers, and an answer belongs_to report. The nested form is displayed and works, but for the 5 answers I have
Add Answer
Answer __
Add Answer
Answer __
[next 3]
How can i group them in one single block with title "Add Answer", and 5 inputs ?
form do |f|
f.inputs "New Report" do
f.input :title
f.input :type
f.input :help_text
f.input :text_box_present
f.input :text_box_default
f.input :template_id
end
f.inputs "ADD Answers", for: :answers do |a|
a.input :text, label: "Answer", required: false
end
f.actions
end

Wow, this is some elegant stuff that active admin and formtastic provides for nested attributes
f.inputs "Answers" do
f.has_many :answers do |a|
a.input :text, label: "Answer", required: false
end
end
It will auto add a delete/add button with javascript, for adding multiple answers

Related

How do I modify the form url with the ActiveAdmin Ruby gem?

I'm using the ActiveAdmin ruby gem, which uses formtastic.
I have a form like below:
ActiveAdmin.register User do
permit_params :first_name, :last_name, :age
form do |f|
input :first_name
input :last_name
input :age
actions
end
end
I want to modify the form URL to a custom URL which includes the user's ID (eg 7). Something like:
ActiveAdmin.register User do
permit_params :first_name, :last_name, :age
form url: "/api/customer/7/attempt", method: :post do |f|
input :first_name
input :last_name
input :age
actions
end
end
The above works (that is, the form url is changed). However, I'm hardcoding the user ID 7 in the URL. I need to do a variable interpolation. But I'm not sure I have access to the user object outside the block.
I have access to the user object within the block like
f.object.id
But not outside the block.
How do I overcome this problem?
Thank you.
I don't know if that's possible, I tried approaches like
resource.id
user.id
params[:id]
and they didn't work.
To solve this problem you can create custom form
form partial: 'form'
and specify url there.
I'm working with Rails 5 and ActiveAdmin with Arctic_Admin them and I found myself in a very similar situation where I need to customize the form action attribute. So, the best way I found to achieve this was by creating a partial form in the corresponding view's folder.
First tell your resource you are going to use a custom form, so add this line to your resource file:
# app/admin/organizations.rb
form partial: "form"
Now you can create your partial by using Arbre Components, like this example:
# app/views/admin/organizations/_form.html.arb
active_admin_form_for [:admin, resource] do |f|
tabs do
tab 'General Configuration' do
f.inputs 'Organization Details' do
admin_accounts = User.with_role(:admin).order('email ASC')
site_collection = Site.where("subdomain <> ''").map { |site| ["#{site.subdomain}", site.id ]}
f.input :name
f.input :kind, :as => :enum
f.input :carereceiver_kind, :as => :enum
f.input :account_manager, :as => :select ,:collection => admin_accounts
f.input :site_id, as: :select ,collection: site_collection
f.input :office_phone
f.input :office_fax
f.input :office_address
f.input :company_logo, :as => :file
f.input :letterhead
f.input :base_url, label: 'BASE URL'
f.input :dynamics_url, label: 'DYNAMICS URL'
f.input :genoa_site_id, label: 'GENOA SITE ID'
end
f.inputs 'Organization Settings' do
f.input :demo_account
f.input :appointment_not_started_notifications_enabled
f.input :erx_enabled
f.input :patients_can_book_appointments
f.input :new_providers_can_manage_their_own_availability_by_default
f.input :clarity_enabled
f.input :whitelist_enabled
f.input :bed_form_enabled
f.input :patient_email_required, label: 'Require patient email addresses'
f.input :patient_credit_card_required, label: 'Require patient credit card information'
f.input :enable_patient_survey, label: 'Enable patient survey'
f.input :enable_provider_survey, label: 'Enable provider survey'
f.input :rcopia4_enabled, label: 'Rcopia 4 enabled'
f.input :share_notes_across_providers_enabled
f.input :d2c_mode
f.input :sso_login_only
f.input :allow_invited_patients_to_complete_profile
f.input :allow_overlapping_appointments, as: :select
f.input :media_mode, :as => :enum
end
f.inputs('Organization Contacts', { class: 'organization_contacts_section' }) do
saved_contacts = f.object.organization_contacts.count
n = 5 - saved_contacts.to_i
n.times do
f.object.organization_contacts.build
end
contact_counter = 0
f.fields_for :organization_contacts do |m|
contact_counter = contact_counter + 1
m.inputs "Contact #{contact_counter}" do
m.input :name, placeholder: 'Name'
m.input :title, placeholder: 'Title'
m.input :email, placeholder: 'Email'
m.input :phone_number, placeholder: 'Phone Number'
end
end
end
end
tab 'Appointment Parser Configuration' do
f.inputs 'Appointment Parser Configuration' do
f.input :appointment_parsers, as: :select, input_html: { multiple: true }
end
end
tab 'EMR Settings' do
f.inputs 'Settings' do
f.input :emr_integrated, label: 'Enable EMR integration'
f.input :emr_processor_type, as: :select, collection: Organization::AVAILABLE_EMR_PROCESSORS
f.input :send_to_emr, label: 'Enable integration from 1DW to EMR'
f.input :receive_from_emr, label: 'Enable integration from EMR to 1DW'
end
end
tab 'Athena EMR Settings' do
f.inputs 'Settings' do
f.input :athena_practice_id, label: 'Athena Practice ID', input_html: { disabled: !f.object.has_athena_processor? }
f.input :athena_department_number, label: 'Athena Department ID', input_html: { disabled: !f.object.has_athena_processor? }
end
end
end
f.actions
end
As you can see with admin_organization_path you can point the URL to any other you want but also you can customize the method to send the form.
Also make sure you use resource for the active_admin_form_for block because you will get an error if you try to use something like #organization.
The form contains a possible way to add nested resources to the principal one, every time you had configured the corresponding model relationships.
And with this, it should be working fine.
I hope it could be useful for someone else. 😃
resource.id should work in your case

Remove "Add new" button from ActiveAdmin has_many form helper

I have nested resources in active admin edit page, but I would like only to allow admin to edit content of existing resources, not to add new nested resources. My code looks like that:
form do |f|
f.inputs do
f.input :author
f.input :content
f.has_many :comments do |comment_form|
comment_form.input :content
comment_form.input :_destroy, as: :boolean, required: false, label: 'Remove'
end
end
f.actions
end
But it add's "Add new comment" button under inputs. How can I disable it, and leave only f.actions buttons for main form?
Starting from v0.6.1 you can pass new_record: false to hide the "Add new" button
f.has_many :comments, new_record: false do |comment_form|
...
end
The commit 4b58b8

manipulating the h3 title with an activeadmin has_many form

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

activeadmin: adding delete for a nested resource

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

rails ActiveAdmin nested form has_one accepts_attributes_for formtastic issue

I am using ActiveAdmin and Rails 3.1 -- having problem understanding whether the following is a bug, or if there is some way to do it correctly that I am not understanding. I am trying to use a nested model with a has one relationship, so that I can create a page and fill out it's meta data in 1 step. --
(page has_one meta_data, accepts_nested_attributes_for meta_data)
Example 1)
in this example, when I click new page, meta data section is there but there are no input fields -- also, if I edit the record, it shows up correctly, however the fieldset is duplicated in the second section... and if I remove the f.inputs wrapping semantic_field_for (which would make sense), then it breaks completely and shows nothing in the meta data area...
form do |f|
f.inputs "Page Information" do
f.input :name
f.input :uri
f.input :view
f.input :body, :as => :text
f.input :active
end
f.inputs "Meta Data" do
f.semantic_fields_for :meta_data do |meta_form|
meta_form.inputs :title, :description, :keywords, :name => "Meta Information"
end
end
end
I understand the meta data probably isn't being instantiated, but I am not sure how I am supposed to do that in the form block? (or if I can even do it) -- The only way I am able to get this to work is by doing using a custom form, and building the meta data in the view, which looks like this
2) How I am working around it, but seems hacky
<%= semantic_form_for [:admin, #page] do |f| %>
<% #page.build_meta_data %>
<%= f.inputs :name => "Page Information" do %>
<%= f.input :name %>
<%= f.input :uri %>
<%= f.input :view %>
<%= f.input :body, :as => :text %>
<%= f.input :active %>
<% end %>
<%= f.semantic_fields_for :meta_data do |meta_form| %>
<%= meta_form.inputs :title, :description, :keywords, :name => "Meta Information" %>
<% end %>
<%= f.buttons %>
<% end %>
Thanks in advance for any help or clarification.
(note to moderators I started another thread on this but was not as clear and didn't have the workaround solution I do now yet, so if one of the questions should be deleted please delete the other)
I found a better solution for you. You can use :for option in inputs helper.
f.inputs "Meta Data", for: [:meta_data, f.object.meta_data || MetaData.new] do |meta_form|
meta_form.input :title
meta_form.input :description
meta_form.input :keywords
end
I think this might work too, but I didn't check
f.inputs :title, :desctiption, :keywords,
name: "Meta Data",
for: [:meta_data, f.object.meta_data || MetaData.new]
In rails 4, this is something that works, with a nice design
e.g.,
A customer has one account
model/customer.rb
accepts_nested_attributes_for :account
admin/customer.rb
form do |f|
f.inputs do
f.input :user, input_html: { disabled: true }
f.input :name
f.input :address
f.input :city
f.input :country, as: :string
end
f.buttons
f.inputs "Account Information", for: [:account, f.object.account] do |s|
s.input :active, as: :boolean
s.input :subscription, as: :boolean
s.input :expires_on, as: :datepicker
s.actions
end
end
controller do
def permitted_params
params.permit!
end
end
end
i was having the same problem, i worked in your hack and got it working.
i then moved <% #page.build_meta_data %> to a custom new method like this
controller do
def new
#tenant = Tenant.new
#tenant.build_tenant_configurable
end
end
hope this helps

Resources