Adding a class to Formtastic radio button - ruby-on-rails

I have a Formtastic radio button collection which is populated by a number of types:
<%= f.input :type_id, :validate => true, :as => :radio, :collection => Type.where(:object_type => 'Post') %>
However, I want to conditionally add a class to each choice, since some of the radio buttons need to be disabled depending on a certain condition.
For example, a :member_class => param would be ideal, but I don't believe it exists like :member_label and :member_value.
Does Formtastic have the ability to allow this?

<%= f.input :type_id, :validate => true, :as => :radio,
:collection => Type.where(:object_type => 'Post'),
:input_html => { :class => (:class_name if condition_goes_here) } %>
or
<%= f.input :type_id, :validate => true, :as => :radio,
:collection => Type.where(:object_type => 'Post'),
:input_html => { :disabled => (:disabled if condition_goes_here) } %>

Related

How can i save check boxes value to database in active admin?

I am new to ruby on rails and active admin, I need to create check boxes for registration fees. while I checked, the value from checkbox is not saved to the database. can anyone help me ??
form do |f|
f.inputs do
f.input :name, label: "Student Name"
f.input :dob,:label => "Date of Birth"
f.input :age,:label => "Age"
f.input :gender, as: :radio, :label => "Gender", :collection => [ "Male", "Female"]
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| ["#{v.reg_fee}"]}
end
f.actions
end
From your code, you are taking array of arrays, instead you should take array of strings,
So, change
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| ["#{v.reg_fee}"]}
to,
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| "#{v.reg_fee}"}
form do |f|
f.inputs do
f.input :name, label: "Student Name"
f.input :dob,:label => "Date of Birth"
f.input :age,:label => "Age"
f.input :gender, as: :radio, :label => "Gender", :collection => [ "Male", "Female"]
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| "#{v.reg_fee}"}
end
f.actions
end

How to fill the fields of the form based on the value selected?

I have created a form in activeadmin as follows. When I select the bkid of the customer, the fields customer name, mobile number, total amount, and amount paid have to be filled automatically in the form.
form :class => 'form_inline' do |f|
f.inputs do
f.input :bkid, :label => 'Booking Id', :as => :select, :collection => Package.all.map{|u| ["#{u.bkid}"]}
f.input :firstName, :as => :string, :label => 'First name', :class => 'same'
f.input :lastName, :as => :string, :label => 'Last name', :class => 'same'
f.input :mobile, :as => :string, :label => 'Mobile no', :class => 'same'
f.input :totalAmount, :as => :string, :label => 'Total Amount', :class => 'same'
f.input :amountPaid, :as => :string, :label => 'Amount Paid', :class => 'same'
f.input :currentPayment, :as => :string, :label => 'Current Payment', :class => 'same'
# f.input :accountcode_id, :label => 'Acc_code', :as => :select, :collection => Accountcode.all.map{|u| ["#{u.accountcode}, #{u.accountname}"]}
br
f.submit :value => 'submit'
end
end
How can I achieve this using ajax?

Simple Form collection, but exclude a specific record

Here is the code I use to generate a select list of Companies.
<%= f.association :company, :collection => Company.order('name ASC'), :label => "Company: ", :include_blank => false %>
Now the table is pre-populated with a special record "id:1, name:none", I want to exclude this record as a selectable option in the select list that's generated. How can I go about accomplishing this?
Thanks!
Try this
<%= f.association :company, :collection => Company.where("id != 1").order('name ASC'), :label => "Company: ", :include_blank => false %>
Or
In the controller
#companies = Company.where("id != 1").order('name ASC')
In the view
<%= f.association :company, :collection => #companies, :label => "Company: ", :include_blank => false %>

Rails simple form checkbox default to true

How do I make a simple for boolean checkbox default to true?
assign_client is a boolean field.
I tried these:
<%= f.input :assign_client, :label => 'Charge Client?', :true %>
<%= f.input :assign_client, :label => 'Charge Client?', :value => :true %>
<%= f.input :assign_client, :label => 'Charge Client?', :value => 1 %>
Thanks for the help!
I think you should add input_html:
<%= f.input :assign_client, :label => 'Charge Client?', :input_html => { :checked => true }
proof
Your second one will work fine just remove the : so it's a boolean value rather than a symbol.
<%= f.input :assign_client, :label => 'Charge Client?', :value => true %>

Rails ActiveAdmin, two columns (not sidebar) on form page

In a form in active admin, the fields expand horizontally across the page according to the size of the window. If it's a big monitor, there is a lot of "unused" space to the right.
How can I add a "column" (NOT a side bar) to the right of the page so that I will end up with a 50% width section of the form on the left and a 50% width section of the form on the right?
I need this because I have lots of fields.
This is what my form partially looks like right now...
form do |f|
f.inputs "Shipment Details" do
f.input :file_number
f.input :customer, :label_method => :company_name
f.input :shipper, :label_method => :company_name
f.input :broker, :label_method => :company_name
end
f.inputs "Places" do
f.input :place_of_origin, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
f.input :place_of_loading, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
f.input :place_of_delivery, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
f.input :via, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
end
f.inputs "Carrier" do
f.input :carrier, :label_method => :company_name
f.input :mode, :as => :select,
:collection => ["Air", "Air Collect", "Air Prepaid", "FCL", "FTL", "LCL", "LTL", "TBA"]
f.input :mbl, :label => "MBL"
f.input :hbl, :label => "HQL"
f.input :vessel
f.input :container
end
f.buttons
end
It's pretty simple, you can give each section of your inputs a CSS class, then you would modify active_admin.css.scss (or just .css) so it would float your forms to the correct place, as well as give them a correct width, etc.

Resources