2009-12-05 1 views
2

조건에 따라 값만 표시하도록 Formtastic 선택을 지시하려면 어떻게합니까?Formtastic에서 조건부 값 표시

- semantic_form_for @member do |f| 
    - f.inputs do 
    = f.input :person 
    = f.input :role, :include_blank => false 
    = f.input :active 

나는 사람 입력이 활성 인 사람을 나열/선택하도록합니다. person.active == true. 조건 해시 맵을 사용하지 않으려 고 시도했습니다.

답변

2

이것은 두 단계 과정입니다.

먼저 활성화 된 사람 만 선택하는 방법이 필요합니다. 다음으로 : collection 옵션을 통해 적극적인 사람들의 컬렉션을 formtastic 입력에 전달해야합니다.

액티브 한 사람 만 선택하는 단계 : 이것은 Person.find(:all, :conditions ["active = ?", true])과 같이 간단합니다. 그러나 이것은 모델의 명명 된 범위를 사용하면 더 잘 수행 될 수 있다고 생각합니다. 수집 옵션 :

- semantic_form_for @member do |f| 
    - f.inputs do 
    = f.input :person, :collection => Person.active 
    = f.input :role, :include_blank => false 
    = f.input :active 
0

당신은을 통해 값의 사용자 지정 컬렉션을 제공 할 수

class Person < ActiveRecord::Base 
    # ... 
    named_scope :active, :conditions => {:active => true} 
end 

지금 Person.active는 업데이트 양식 Person.find(:all, :conditions ["active = ?", true])

2 단계와 동일합니다 f.input :person, :collection => Person.find(:all, :conditions => "whatever")