Active Admin remove certain fields while going for updation - ruby-on-rails

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

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

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

rails activeadmin ask to show a input field if new

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

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

Creating a record with Associations - Active Admin

I am using active admin and seem to be struggling with creating a record with an association. I have set this up in the normal way
class Membership < ActiveRecord::Base
belongs_to :member
attr_accessible :membership_type
end
class Member < ActiveRecord::Base
has_one :membership
accepts_nested_attributes_for :membership
attr_accessible :membership_attributes, :forename, :middlename, :surname, :house_no, :house_name, :street, :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email
end
I then want to create a New member along with their membership type in the same form, so my member.rb looks like this so far
ActiveAdmin.register Member do
# Set Which Columns are to be displayed on the index page
index do
column :forename
column :middlename
column :surname
column :house_no
column :house_name
column :street
column :town
column :postcode
column :home_tel
column :mobile_tel
column :work_tel
column :email
column :membership do |member|
member.membership.map{ |ms| ms.membership_type}
end
default_actions
end
# Set Which Columns are to be displayed on Create New Member
form do |f|
f.inputs "Member Registration" do
f.input :forename
f.input :middlename
f.input :surname
f.input :house_no
f.input :house_name
f.input :street
f.input :town
f.input :postcode
f.input :home_tel
f.input :mobile_tel
f.input :work_tel
f.input :email
end
f.inputs :for => [:membership, f.object.membership || Membership.new] do |m|
m.input :membership_type, :label => 'Membership Type', :as => :select, :collection => Membership.all.map{|m| [m.membership_type]}
end
f.actions
end
end
This displays a select box where i can choose from various Membership types, but i dont seem to be passing the correct value when creating the record,
This is an example of what is being posted
member[forename]:Name 1
member[middlename]:Name 2
member[surname]:Name 3
member[house_no]:1
member[house_name]:
member[street]:Test Street
member[town]:Test Town
member[postcode]:CF23 7BD
member[home_tel]:01633222222
member[mobile_tel]:07864582356
member[work_tel]:01633555555
member[email]:test#mail.com
membership_attributes"=>{"membership_type"=>"Student"}
commit:Create Member
This doesn't seem right does it? Also when i try and view the record
column :memberships do |member|
member.memberships.map{ |ms| ms.membership_type}
end
I get the following mysql error
Mysql2::Error: Unknown column 'memberships.member_id' in 'where clause': SELECT `memberships`.* FROM `memberships` WHERE `memberships`.`member_id` = 10
Could anyone point me in the right direction please or can anyone see where im going wrong?
Much appreciated
Instead of f.input :memberships try this:
f.has_many :memberships do |pf|
pf.input :membership_type
end
and close your member object fields like this
f.inputs "Member Registration" do
f.input :forename
f.input :middlename
f.input :surname
...
end
So your form should look like this:
form do |f|
f.inputs "Member Registration" do
f.input :forename
f.input :middlename
f.input :surname
# .. the rest of your fields
end
f.has_many :memberships do |pf|
pf.input :membership_type
end
f.actions
end
Regarding the MySQL error, i think for the association to work you need to add the column member_id to the memberships table. You can create the migration with
rails g migration add_member_id_to_memberships member_id:integer
Then migrate with
bundle exec rake db:migrate

Resources