rails activeadmin ask to show a input field if new - ruby-on-rails

I have a form in ActiveAdmin:
form do |f|
f.inputs t('out_payment_details') do
f.input :amount
f.input :status
f.input :project
f.input :approved_user
f.input :request_user
f.input :from_company
f.input :to_company
end
f.actions
end
What I want:
When new/create, request_user is always the current_user, and if edit(not create), then the approved_user is current_user.
How can I have some conditions of default value with sth like "on new form"?

By using if and else condition like
if f.object.new_record?
input field
else
input field
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"

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

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

ActiveAdmin: Generating drop down list in form for has_many/belongs_to relationship

I have Gallery and Image models with has_many/belongs_to relationship.
My admin_active form for Image looks like this:
form do |f|
f.inputs "Image Details" do
f.input :gallery_id
f.input :file
end
end
Instead or entering number for gallery_id I'd like to chose gallery name from drop-down list. By deafault this form looks like this, but I don't know how to do this manually.
Change your form to the following
form do |f|
f.inputs "Image Details" do
f.input :gallery_id, as: :select, collection: Gallery.select(:name).uniq
f.input :file
end
end
Try this
form do |f|
f.inputs "Image Details" do
f.input :gallery
f.input :file
end
end

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