Basically, I'm trying to create a dynamic group of check boxes that are keyed off a category select in the same form. For example, a user would choose a category from the select, and then a list of corresponding subcategory checkboxes would appear.
I've done this before with only selects, but those are much easier because you only have to supply a generic set of options. Check boxes (especially with formtastic) have a lot of corresponding markup that I'd rather not generate myself.
My question, then, is how to get formtastic to create only the proper check boxes but still have their name and id fields contain all the correctly nested information. I want it to act exactly like the following, but only output the last line for me to send over ajax.
= semantic_form_for #user do |u|
= u.inputs :name, :age
= semantic_fields_for :job do |f|
= f.input :category, :as => :select, :collection => Category.all
= f.input :subcategory, :as => :check_boxes, :collection => # This is what needs to be dynamic
I've tried just using the last line wrapped in a generic semantic_fields_for, but the field names are no longer correct.
How would you do it?
I would try this:
= semantic_fields_for #user do |u|
= semantic_fields_for :job do |f|
= f.input :subcategory, :as => :check_boxes, :collection => some_thing
I'm pretty sure that should work.
You'd need to use a helper and define it as a method. eg:
def form_boxes(f)
f.input :subcategory, :as => :check_boxes, :collection => stuff_goes_here
end
and then call in your view:
= form_boxes(f)
Related
Model
# certification.rb
class Certification < ActiveRecord::Base
extend Enumerize
enumerize :certification_type, in: [:SEO, :CRM]
end
My admin file
# admin/certification.rb
ActiveAdmin.register Certification do
permit_params :name,
:certification_type,
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Certifications" do
f.input :name, :label => 'Nom'
f.input :certification_type, :label => 'Type',
as: :check_boxes
end
f.actions
end
end
The problem is with the certification_type field.
When I tick one type in my activeadmin page, the entry isn't saved in the database. But when I change as: :check_boxes with a as: :select, it works.
Do you know if there is a reason ?
Thank you
You cannot use checkboxes here, as checkboxes allow to select multiple values for one field, but you didn't specify multiple: true on the enumerize (because you don't need this, I guess). So you should use radio buttons, as they allow to select only one of many values (similar to select).
Try to change as: :check_boxes to as: :radio:
f.input :certification_type, :label => 'Type', as: :radio
In my Rails application, I have the following model:
class Idea < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :ideas
end
I am creating ActiveAdmin CRUD for my Idea model with the custom form that looks something like that looks something like that:
form do |f|
f.inputs do
f.input :member
f.input :description
end
end
The requirement is to have the custom text for a content of the member association, i.e "#{last_name}, #{first_name}". Is it possible to customize my member select box to achieve it?
Any help will be appreciated.
Yes, that is possible. I assume you want to use a DropDown list box for members to select a user from User model.
form do |f|
f.inputs do
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}
f.input :description
end
end
For Active Admin You have to pass it as a collection of Hashes. Key in hash will be the text which you want to display and value will be the attribute id.
For Example:
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}.to_h
collection: [{name1: 1}, {name2: 2}, {name3: 3}]
Note: I have added to_h at end of the map which will convert a collection of arrays into a collection of hashes.
New formtastic user here.
I have a relationship user has_many clients
In formtastic, if I do something such as
f.input :employer
it returns a select box of all employer object references. I'd like to display (last name, first name) instead. I'm sure this is very simple, but I can't figure out exactly how to do it.. Any help appreciated.
These didnt work for me, however, this did:
<%= f.input :user, :label => "Author", :label_method => :username %>
Also a little cleaner ^^
Or, you can set the display method once and for all on the model itself:
(In employer.rb)
def to_label
email
end
Try
f.input :employers, :as => :select, :collection => Employer.find(:all, :order => "last_name ASC")
or
f.input :employers, :as => :select, :collection => Employer.collect {|e| [e.last_name, e.id] }
I'm new to rails and I guess you can answer this question easily.
What I got so far is
= f.input :task, :as => :select, :collection => #tasks, :include_blank => true
where the tasks collection is defined by
Task.find(:all)
within in the controller.
This does in fact work, shows me a dropdown-list of all Tasks to select from and connects them.
The problem here is, that the dropdown menu shows me values like
#<Task:0x123456789d1234>
Where do I define what value is being displayed?
I believe you can use the :label_method to solve your problem...
f.input :task, :as => :select, :collection => #tasks,
:include_blank => true, :label_method => :title
where :title is what you want to show.
This may help a little more.
You can define a to_s method in the model:
class Task < ActiveRecord::Base
def to_s
title # the attribute to display for the label
end
end
How do I instruct Formtastic select to only display values based on a condition?
- semantic_form_for #member do |f|
- f.inputs do
= f.input :person
= f.input :role, :include_blank => false
= f.input :active
I only want the :person input to list/select people who are active i.e. person.active == true. I tried to pass a condition hash map to no avail.
This is a two step process.
First you need a means of selecting only People that are active. Next you need to pass that collection of active people to the formtastic input via the :collection option.
Step one selecting only active people: This is as simple as Person.find(:all, :conditions ["active = ?", true]). But I think this is better accomplished with a named scope in the model.
class Person < ActiveRecord::Base
# ...
named_scope :active, :conditions => {:active => true}
end
Now Person.active is the same as Person.find(:all, :conditions ["active = ?", true])
Step two, updating the form:
- semantic_form_for #member do |f|
- f.inputs do
= f.input :person, :collection => Person.active
= f.input :role, :include_blank => false
= f.input :active
You can supply any custom collection of values through the :collection option:
f.input :person, :collection => Person.find(:all, :conditions => "whatever")