Rails Activeadmin : Check boxes values are not saved - ruby-on-rails

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

Related

belongs_to id not being set activeadmin

I have two models: Asssessment and Question which are organized like this:
class Question < ActiveRecord::Base
belongs_to :assessment
class Assessment < ActiveRecord::Base
has_many :questions
I'm trying to create an activeadmin (ver 1.0.0) interface to create assessments and add questions to them.
So far I've tried making a questions tab:
ActiveAdmin.register Question do
permit_params :question_text, :question_type, :scale_min, :scale_max
form do |f|
f.inputs "Question Information" do
f.input :assessment, :as => :select, :collection => Assessment.non_daily_assessments
f.input :question_type, :as => :select, :collection => Question.human_readable_question_types.keys
f.input :question_text, :input_html => {:rows => 2, :cols => 10}
f.input :scale_min
f.input :scale_max
end
f.actions
end
non_daily_assessments simply returns a subset of all assessments
I am able to select from a list of assessments, but when I save the question and am taken to the "view question" page the question's assessment_id is empty.
Similarly, if I create an assessments tab:
ActiveAdmin.register Assessment do
permit_params :name, :questions
form do |f|
f.inputs "Assessment Information" do
f.input :name, :input_html => {:rows => 1, :cols => 10}
f.has_many :questions, :allow_destroy => true, :heading => 'Questions' do |qf|
qf.input :question_type, :as => :select, :collection => Question.human_readable_question_types.keys
qf.input :question_text, :input_html => {:rows => 2, :cols => 10}
qf.input :scale_min
qf.input :scale_max
end
end
f.actions
end
I am able to go to a particular assessment and start adding questions, but when I reload the page they're gone. Going into the console I see that the questions were created, but their assessment_id's are nil just like through the question tab.
What is a correct way to create an activeadmin interface for a belongs_to has_many relationship?
Let me know if you need more information.
Your permit_params are incomplete. Have a look at this answer: Nested form in activeadmin not saving updates
You need to add :assessment_id to the permit_params in the Question section, and if you want to be able to edit questions with the assessments, you're probably missing the accepts_nested_attributes_for :questions in the Assessment model, and you'll also need to change the permit_params in the Assessment section to something like
permit_params :name, questions_attributes: [:id, :question_type, :question_text, :scale_min, :scale_max]

Active Admin has_many selectable list of records

I have been trying for days now, I am new at ROR and active admin. So far I have been able to add and delete has_many relations for a new record. And I am using strong_parameters as well as accept_nested_attributes. I want the
Ability to add and delete relations for existing records as well.
Ideally there should be an autocomplete box that allows searching and selection of an existing meaning for this particular model.
My models are
Word
Meaning
WordMeaning
I only want the capability to attach meanings that are already available to a word?
class Word < ActiveRecord::Base
belongs_to :language
has_many :word_meanings
has_many :meanings ,through: :word_meanings
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs do
f.input :language
f.input :word
f.input :wordInScript
f.input :pronunciation, :required => false, :as => :file
end
f.inputs do
f.has_many :meanings, heading: 'Meanings', allow_destroy: true, new_record: true do |a|
a.input :meaning
a.input :language
end
end
f.actions
end
You can determine the collection of the select:
a.input :meaning, :as => :select, :collection => {#your collection in Hash, Array or ActiveRecord relation}.map{|key, value| [value, key] }
ActiveAdmin uses Formtastic:
https://github.com/justinfrench/formtastic#usage

Rails Activeadmin - custom association select box

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.

How to create Formtastic "partials"

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)

Rails: How to use select in formtastic with activeRecord

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

Resources