Set multiple attributes on basis of results of a :select in rails form - ruby-on-rails

I have a select box on my form which retreives an object with the following attributes:
:id, :v2_read_code, :v2_term
The select code is:
f.inputs "Tests" do
f.has_many :eqa_material_tests, :allow_destroy => true, :heading => 'Tests In EQA Material' do |cf|
cf.input :test_id, :as => :select, :collection => Hash[Test.all.order(default: :asc).map{|b| [b.v2_term,b.id]}]
end
end
Where I am storing the test id in a model/table with the following structure:
eqa_material_tests
id, test_id, eqa_material_id
In addition to storing the test_id, I'd also like to store the v2_read_code and v2_term as I'd like to keep a copy of these items if possible.
Is this possible?

After a nights sleep I've realised I'm approaching this wrong. I can do this using an active record callback like after_create

Related

Active Admin Filters no viewing

So I'm in Active Admin and in the following model a Degree belongs to a user and a User has many degrees
For a User we have a site_id that identifies where the User works in a Site table
So in my filter in Active Admin's degree model, I can't seem to pull a list of sites that someone can filter from in the Active Admin UI.
Here are my filters so far:
major
institution
completion_date
These are straight from the Degree tables
:user_active comes from Users table with a boolean attribute for an an Active
ActiveAdmin.register Degree do
belongs_to :user, :optional => true
menu :parent => 'Users'
config.sort_order = 'users.last_name_asc'
filter :user_active, :as => :select
**filter :site_id, collection: -> { User.all }, label: 'sites'**
filter :degree_type
filter :major
filter :institution
filter :completion_date
I've tried this as well
filter :site_id, :as => :select, collection: -> { User.all }, label: 'sites'
and no error message
I've also tried something like this with no error message, but nothing in the UI comes through again
filter :site, label: "Site", :as => :select, :collection => User.site_id
I've tried this and get the following errors:
filter :site_id, label: "Site", :as => :select, :collection => User.all
undefined method `site_id_eq' for Ransack::Search<class: Degree, base: Grouping <combinator: and>>:Ransack::Search
Any help here?
Update
I did some work-around and this is what I end-ed up with
filter :user_site_id, label: "site", :as => :select, :collection => User.all.map{|u| u.site}.map{|s| s.city if s.present? }.uniq.compact
The collection says
Iterated through all the users and call u.site (because a site_id
belongs to a User so there's a Site table that'll take each instance
of a user)
Since we're now at each Site object, map each of those
values and retrieve the city name for each Site object if it's
present...
I want to only filter the uniq values and .compact is
getting rid of the nil values in the array
I think there's a better solution because I'm going through so many iterations? Anyone have any other ideas? I'm going to make this an instance method on the Users model after the refactor.
Try this out:
filter :site_id, as: :select, collection: -> { User.pluck(:site_id) }, label: 'Sites'

Create a drop down menu using a Table Attribute in Rails Active Admin

I need to create a dropdown field(membership_code), whose values are contained on a different table called members.
Schema
prereg
id
membership_code(string) not a foreign key
verification_code
members
id
membership_code
Prereg Active Admin Model
ActiveAdmin.register Prereg do
form do |f|
f.inputs "Preregistered Users" do
f.input :verification_code
f.input :email
#THIS LINE NEEDS TO BE CHANGED TO LIST DOWN THE MEMBERSHIP_CODE FROM MEMBERS
# f.input :membership_code, :as => :select, :collection => Members.all()
end
f.actions
end
To add, I was planning to have this logic wherein whenever you create a Prereg record, the selected "membership_code" would be deleted from the members.membership_code list.
How is this done in ActiveAdmin? Sorry I haven't found any good resource for DB Hooks and I'm still new to Rails.
I think you are looking for something as follows:
f.input :membership_code, as: :select, collection: Member.all.map(&:membership_code)
try this
f.input :membership_code, :as => :select, :collection => Members.select(:membership_code)
Thanks

Collection select with many-to-many in same model in rails gives error

I have a weird rails database/form problem.
I got 1 table with Courses, and 1 table Prerequisites, which has 2 columns that both hold Course_ids (columns are called "course_a_id" and "course_b_id").
In the Course model I have this:
:has_and_belongs_to_many(:prerequisites,
:join_table => "prerequisites",
:foreign_key => "course_a_id",
:association_foreign_key => "course_b_id",
:class_name => "Course")
From this SO answer
This works if I put the prerequisites in by Console, something like this:
Course.find(3).prerequisites = [Course.find(1), Course.find(2)]
This form field however does not let me put prereqs in the DB:
<%= f.label :prerequisite, "Prerequisites" %>
<%= f.collection_select(:prerequisites, Course.all, :id, :name,
{:multiple => true}, :multiple => "multiple") %>
Gives this error after posting with one option selected with value 1:
"Course(#-631146998) expected, got String(#77208170)"
and in the parameter dump:
"prerequisites"=>["",
"1"]}
I have no clue how that "" ended up in the params and can't figure out another way to create the form field. I think my model is set up correctly.

Active Admin - refresh second drop down based on first drop down, Ruby on Rails

I am using Active Admin Gem on Ruby on Rails. I have a form in which i have selected category and sub category and then accordingly i have to fill the data. So i created two tables in sqlite added in active admin resouce.
Every thing is working fine but the drop down of sub category is not getting filtered based on the category choosen.
I am new to Ruby and RoR too. I don't know how to refresh dropdown of the subcategory after selecting category.
I know i can do it from AJAX and javascript but i dont know where to code for that?
Also, is there any specific filter avaliable in Active Admin which will make it happen without ajax or javascript.
Any ideas or help will be highly appreciated.
i don't know if there is any specific filter avaliable in Active Admin, but i solved it in this 3-steps way (assuming category - is a house, subcategory - is a flat):
1-st step: define helper containing ajax request
(of course, you have to predefine path in routes.rb)
#application_helper.rb
def remote_request(type, path, params={}, target_tag_id)
"$.#{type}('#{path}',
{#{params.collect { |p| "#{p[0]}: #{p[1]}" }.join(", ")}},
function(data) {$('##{target_tag_id}').html(data);}
);"
end
2-nd step: add this method for :onchange action
#admin/inhabitants.rb (DSL with formtastic)
form do |f|
f.inputs do
#...
f.input :house, :input_html => {
:onchange => remote_request(:post, :change_flats, {:house_id=>"$('#house_id').val()"}, :flat_id)
}
f.input :flat
#...
end
end
3-rd step: render result of filtering
(you can render partial instead of :text, I decided leave it in one activeadmin resource file )
controller do
def change_flats
#flats = House.find_by_id(params[:house_id]).try(:flats)
render :text=>view_context.options_from_collection_for_select(#flats, :id, :flat_number)
end
end
I accomplished this as any non-rails developer working on a rails project would - quick and dirty. Here's how:
#...
f.input :user, :input_html => {
:onchange => "
var user = $(this).val();
$('#order_location_id').val(0).find('option').each(function(){
var $option = $(this),
isCorrectUser = ($option.attr('data-user') === user);
$option.prop('disabled',!isCorrectUser);
});
"
}
f.input :location, collection: Location.all.map{ |loc|
[loc.name,loc.id, {"data-user" => loc.user_id}]
}
#...
No AJAX required. Note that this does not remove the unwanted options, it just disables them (sufficient for my scenario). This could easily be made modular with a helper, but I really only needed the functionality once.
For anyone else wrestling with the same problem, look at this railscast
I faced the same problem here
here's how I implemented multiple dynamic select menus in activeadmin:
config/initializers/active_admin.rb
config.register_javascript 'exam_registrations.js.coffee'
app/admin/exam_registrations.rb
form do |f|
f.inputs "Exam Registration Details" do
f.input :user_id, :label => 'Teacher', :as => :select, :collection => User.where(:admin => 'false', :active => true).order(:name), :include_blank => true
f.input :student_id, :hint => 'Students grouped by teacher names', :as => :select, :collection => option_groups_from_collection_for_select(User.where(:admin => false, :active => true).order(:name), :students, :name, :id, :name)
f.input :lesson_id, :hint => 'Lessons grouped by student names', :as => :select, :collection => option_groups_from_collection_for_select(Student.where(:active => true).order(:name), :lessons, :name, :id, :name)
end
f.buttons
end
app/assets/javascripts/exam_registrations.js.coffee
#first menu
jQuery ->
$('#exam_registration_student_id').parent().hide()
students = $('#exam_registration_student_id').html()
$('#exam_registration_user_id').change ->
user = $('#exam_registration_user_id :selected').text()
escaped_user = user.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/#])/g, '\\$1')
options = $(students).filter("optgroup[label='#{escaped_user}']").html()
if options
$('#exam_registration_student_id').html(options)
$('#exam_registration_student_id').parent().show()
else
$('#exam_registration_student_id').empty()
$('#exam_registration_lesson_id').empty()
# second menu
$('#exam_registration_lesson_id').parent().hide()
lessons = $('#exam_registration_lesson_id').html()
$('#exam_registration_student_id').click ->
student = $('#exam_registration_student_id :selected').text()
escaped_student = student.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/#])/g, '\\$1')
options = $(lessons).filter("optgroup[label='#{escaped_student}']").html()
if options
$('#exam_registration_lesson_id').html(options)
$('#exam_registration_lesson_id').parent().show()
else
$('#exam_registration_lesson_id').empty()
restart the server and the menus work!
Now it's possible with this gem https://github.com/holyketzer/activeadmin-ajax_filter, use in you form code like this:
f.input :category_id, as: :select # ...
f.input :subcategory_id, as: :ajax_select, data: {
ajax_search_fields: [:category_id],
search_fields: [:subcategory_atrribute],
url: '/admin/subcategories/filter'
}
And in you subcategory resource page:
ActiveAdmin.register Subcategory do
include ActiveAdmin::AjaxFilter
# ...
end
don't forget to include assets
You can also use activeadmin_addons gem Nested Select
dependent-select could be also a good option

HashWithIndifferentAccess in related models

I've got a rails app where I'm linking fields across two databases. The database stuff all seems to be fine.
However, I have one form where I am mapping a description from the remote database to a product in the local database.
The form the used to create the product and select the description works fine
#_form.rb
semantic_form_for #products do |f|
f.input :name
semantic_fields_for :description_maps do |description|
description.input :desciption_map_id, :input_html=>{:name=>"product[description_map][description_id]}, :collection => #descriptions
end
end
#product.rb
class Product < ActiveRecord::Base
attr_accessible :name, :description_map_attributes, :description_map
has_one :description_map
accepts_nested_attributes_for :description_map
when I submit the form, I get an error
DescriptionMap(#...) expected, got ActiveSupport::HashWithIndifferentAccess(#othernumber)
I can't seem to figure out why this is happening.
the parameters being posted look fine
"product"=>{"name"=>"test name",
"description_map"=>{"description_id"=>"1"}}
turns out this was an issue with how formtastic was naming the description map.
In the question, i had specified
description.input :desciption_map_id, :input_html=>{:name=>"product[description_map][description_id]}, :collection => #descriptions
but the 'description_map' needed to be 'description_map_attributes' like this
description.input :desciption_map_id, :input_html=>{:name=>"product[description_map_attributes][description_id]}, :collection => #descriptions
Hopefully this answer helps somebody else having the same issue.
You should use: :description_map (without "S") cause is a has_on relation
semantic_fields_for :description_map do |description|
description.input :desciption_map_id, :input_html=>{:name=>"product[description_map] [description_id]}, :collection => #descriptions
end

Resources