belongs_to id not being set activeadmin - ruby-on-rails

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]

Related

Rails Activeadmin : Check boxes values are not saved

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

unnecessary text display in active admin "awesome nested set" gem in ROR

I am using awesome nested set gem in ROR and it's working fine, But in add form, unnecessary text is displaying.
#<#<Class:0x00000006fa4c30>:0x00000006f7fe30>
here is my code
Controller
ActiveAdmin.register Category do
permit_params :name, :lft, :rgt, :parent_id, :depth
# Set the default sort order.
config.sort_order = 'lft_asc'
# Add member actions for positioning.
sortable_tree_member_actions
form do |f|
f.inputs do
f.input :parent, :as => :select, :collection => f.template.nested_set_options(Category, #category) {|i| "#{'--' * i.level} #{i.name}" }
#f.input :parent, :as => :select, :collection => Category.where("parent_id IS NULL")
f.input :name
end # f.inputs
f.actions
end # form
index do
# This adds columns for moving up, down, top and bottom.
sortable_tree_columns
sortable_tree_indented_column :name
actions
end
end
Model
class Category < ActiveRecord::Base
# awesome nested set
acts_as_nested_set
validates :name, :presence => true
#default_scope :order => 'lft ASC'
#...
end
f.input :parent, :as => :select, :collection => nested_set_options(Category, #category) {|i| "#{'--' * i.level} #{i.name}" }
remove f.template and it won't print that

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.

Showing error messages in active admin for has many relationship table

I am facing an issue showing up the error messages in active admin.
I get all the error messages displayed with the fields in the form.
But in the code below, I need atleast one skill and maximum 5 skills to be added.
Else need to throw an error message.
I've added a validation in model as :
validates :skills, :length => { :minimum => 1, :maximum => 5,
:message => " should be atleast 1 and less than 5"}
This validates perfectly, but no error message is displayed.
Can anyone help me with the display of the error message.
Following is the code :
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "User", :multipart => true do
f.input :name
f.input :email, :as => :email
f.input :profile_name
f.input :date_of_birth
f.input :gender, :as => :select, :collection => Gender::GENDERS
end
f.inputs "Skills* ( minimum 1 & maximum 5 )" do
f.has_many :skills do |p|
if !p.object.nil?
# show the destroy checkbox only if it is an existing appointment
# else, there's already dynamic JS to add / remove new appointments
p.input :_destroy, :as => :boolean, :label => "Destroy?",
:hint => "Check this checkbox, if you want to delete this field."
end
p.input :description
p.input :title
end
end
end
end
activeadmin 0.5.1 is available on github.
it contains next line in changelog
"Add support for semantic errors #905 by #robdiciuccio"
here is pull request with this feature
https://github.com/gregbell/active_admin/pull/905
example
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs
f.inputs "Locations" do
f.has_many :locations do |loc|
loc.input :address
loc.input :_destroy, :as => :boolean, :label => "Delete"
end
end
f.buttons
end
to use it add to Gemfile
gem 'activeadmin', :git => "git://github.com/gregbell/active_admin.git", :tag => "v0.5.1"
For passing validation try this
validates_length_of :skills,
:within => 1..5,
:too_short => 'too short message',
:too_long => 'too long message'

Resources