I am using the activeadmin gem with formtastic I want to add a datalist to a particular text field.
#FILE app/admin/model.rb
ActiveAdmin.register Model do
permit_params :title, :article_headline_a_id, :article_headline_b_id
form(html: { multipart: true }) do |f|
f.input :article_headline_a_id, :as => :datalist, :collection => Article.pluck(:title)
f.input :article_headline_b_id, :as => :datalist, :collection => Article.pluck(:title)
f.actions
end
end
Everything seems to work fine until I save and check the console I get article_a_id = 0 instead of the expected id number.
#model.rb
belongs_to :article1, :foreign_key => 'article_headline_a_id', :class_name => "Article"
belongs_to :article2, :foreign_key => 'article_headline_b_id', :class_name => "Article"
Sounds like you're not mapping the ID to the title try:
f.input :article_headline_a_id, :as => :datalist, :collection => Article.pluck(:title, :id)
Related
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
I would like to make a dynamic select option via Activeadmin's formtastic like so:
form do |f|
f.inputs "Exam Registration Details" do
f.input :user_id, :as => :select, :collection => User.where(:admin => 'false')
#selects user from list. WORKING
f.input :student_id, :as => :select, :collection => Student.joins(lessons: :user)
#collection of students will change to students who have lessons with chosen user. NOT WORKING, returns all students who have lessons.
f.input :lesson_id, :as => :select, :collection => Lesson.joins(:student, :user)
#collection of lessons will change to reflect lessons connected by chosen user and student. NOT WORKING, returns all lessons.
end
f.buttons
end
My amateurish code is obviously not working as I intended it to. What changes should I make?
I have 4 models as below:
class Student < ActiveRecord::Base
has_many :lessons
has_many :users, through: :lessons
has_many :exam_registrations, through: :lessons
class Lesson < ActiveRecord::Base
belongs_to :user
belongs_to :student
belongs_to :exam_registration
class User < ActiveRecord::Base
has_many :lessons
has_many :students, through: :lessons
has_many :exam_registrations, through: :lessons
class ExamRegistration < ActiveRecord::Base
has_many :lessons
has_many :users, through: :lessons
has_many :students, through: :lessons
SOLVED
For anyone else wrestling with the same problem, look at this railscast
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!
I have a form for creating a Style. When creating the style the user must also choose some features for the style.
The features are pre-populated into a multi-select box inside the form. When the style is saved the through table should be updated with entries (with style_id and feature_id) for each feature selected on the form.
I have in my controller:
def new
#style = Style.new
#style.stylefeatures.build
end
def create
#style = Style.new(params[:style])
#style.stylefeatures.build
#style.save
end
...and in my style model
attr_accessible :stylefeatures_attributes
has_many :stylefeatures
has_many :features, :through => :stylefeatures, :foreign_key => :feature_id
accepts_nested_attributes_for :stylefeatures
... and in my stylefeature model
belongs_to :style
belongs_to :feature
accepts_nested_attributes_for :feature
... and in my feature model
attr_accessible :description, :fullname, :name
has_many :stylefeatures
has_many :styles, :through => :stylefeatures, :foreign_key => :style_id
... and in my create form
<%= m.simple_fields_for :stylefeatures do |p| %>
<%= p.input :feature_id, :label => "Features", :collection => Feature.all, :input_html => { :multiple => true } %>
<% end %>
Now when I save the new Style, the stylefeatures table is updated with the appropriate style_id, but with two useless entries. The first is an array with all of the feature ids which have been selected in the form. The second is a blank entry with the appropriate style_id and nothing in the feature_id column.
Do you have any clue of what I might be doing wrong, or how to spread the collected feature_id's into the table as desired?
in new action u r building only one stylefeature, so only one will be created with feature_id = '1,3,45,563' (ofc depends that features u selected)
second one is generated by #style.stylefeatures.build in create action
u can try to remove fields_for and simply use :feature_ids instead of :feature_id
<%= p.input :feature_ids, :label => "Features", :collection => #features, :input_html => { :multiple => true } %>
or
<%= input_tag "style[feature_ids][]" , :label => "Features", :collection => #features, :input_html => { :multiple => true } %>
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 use the superb Formtastic plugin for Ruby on Rails.
Does anybody know how to include a blank (option), when using a custom collection?
When I try:
<%= f.input :organizations, :collection => Organization.all(:order => :name), :include_blank => true %>
I get the select box with the collection, but NOT a blank...
What kind of association is :organizations? Does it work if you specify :as => :select?
There's spec coverage for the following belongs_to select, date, time and datetime inputs:
f.input(:author, :as => :select, :include_blank => true)
f.input(:created_at, :as => :date, :include_blank => true)
f.input(:created_at, :as => :time, :include_blank => true)
f.input(:created_at, :as => :datetime, :include_blank => true)
My guess is that organizations is not a belongs_to association, right? If it's a :has_many or :has_and_belongs_to_many association, Formtastic will try to do checkboxes or a multi-select. In the case of a multi-select, obviously it makes no sense to have a blank line in there (you just don't select any of the items).
Hope this helps, please post some more details about the models and associations in question.