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")
Related
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]
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.
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)
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