2011-10-14 2 views
0

field : as => : enum에 대해 formtastic ~> 1.2.3의 "monkey patch"와 함께 enumerated_attribute를 사용하고 모든 것이 잘 동작했습니다.Formtastic ~> 2.0.2 및 enumerated_attribute gem, Rails 3.1.1

그러나 formtastic을 2.0.2 버전으로 업데이트하면 "Formtastic :: UnknownInputError"라는 메시지가 나타납니다.

module FormtastiC#:nodoc: 
    class SemanticFormBuilder #:nodoc: 
    def enum_input(method, options) 
     unless options[:collection] 
     enum = @object.enums(method.to_sym) 
     choices = enum ? enum.select_options : [] 
     options[:collection] = choices 
     end 
     if (value = @object.__send__(method.to_sym)) 
     options[:selected] ||= value.to_s 
     else 
     options[:include_blank] ||= true 
     end 
     select_input(method, options) 
    end 
    end 
end 

추신 : 자세한 내용은

여기 /initialisers/formtastic.rb에 추가 된 패치입니다 나는 SemanticFormBuilder를 FormBuilder로 변경하려고 시도했다. (나는 새로운 formtastic 문서에서 모든 커스텀 입력에 대해 그런 변화가 있다는 것을 이해했다.) 그러나 여전히 오류가 발생했다.

아마도이 젬들을 모두 성공적으로 함께 사용 했을까?

답변

1

그들은 방법으로 정의 사용자 정의 필드 Formtastic 2.x는 완전히 변경은

당신은 당신이 원하는 것을 얻을 수있는 내부 Formtastic 클래스를 서브 클래 싱해야합니다. 셀렉트 입력은 다음과 같이 보일 것입니다 :

 
module FormtasticExtensions 
    class EnumeratedInput < Formtastic::Inputs::SelectInput 
    def collection 
     # programmatically build an array of options in here and return them 
     # they should be in this format: 
     # [['name', 'value'],['name2', 'value2']] 
    end 
    end 
end 

가 Formtastic 초기화에서 모듈을 포함한다 :

include FormtasticExtensions

을이 당신에게 필드 :as => :enumerated을 줄 것이다 당신이 갈 수 있어야한다 . 필자의 경우 (다른 사용자 정의 필드) 현재 옵션을 선택하지만 작업 할 코드를 조정해야 할 수도 있습니다. 응답에 대한

f.input :thing, :as => :select, :collection => your_collection, :label_method => :your_name, :value_method => :your_id

+0

감사 :

은 또한 그냥 컬렉션을 전달할 수 있습니다. 마침내 당신의 콜렉션 메소드로 작동하게 만들었지 만, app/inputs/enum_input.rb 파일을 사용하여 EnumInput 클래스