views:

373

answers:

2

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.

A: 

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
EmFi
A: 

You can supply any custom collection of values through the :collection option:

f.input :person, :collection => Person.find(:all, :conditions => "whatever")

Justin French