2013-08-10 2 views
4

JSON 중첩 된 해시에 대한 simple_form을 사용자 지정하는 방법 :좀 중첩 된 데이터가

@preset = Preset.new 
#fields is a postgres json data type 
@preset.fields = {'primary_category' => {'category_id' => 57882}} 

나는 부분 내 양식이가 너무 제출 양식에서 POST params[:preset][:fields]에 보존 같은 중첩 된 구조를 가지고 싶습니다 :

<%= text_field_tag("preset[fields][primary_category][category_id]",nil) -%> 

간단한 양식은 hstore 또는 json 유형과 같은 새로운 유형의 postgres를 처리하는 방법을 알지 못합니다. 필자의 경우에는 유효성 검사 나 데이터 유형을 감지하는 데 정말로 필요하지 않습니다. SimpleForm을 확장하여 열 유형 검색을 건너 뛰고 텍스트 필드에 대해 출력하는 것과 동일한 기존 부트 스트랩 상용구를 출력 할 수있는 방법이 있습니까? 그러나 임의의 json 중첩 키에 대해서만 출력 할 수 있습니까?

어쩌면이 같은 사용 : 나는 연장으로 보았다

<%= f.input 'preset[fields][primary_category][category_id]', :as => :json_text_field %> 
출력 위의 도우미와 같은 일

하지만 라벨에 둘러싸여 및 제어 그룹 분류 된 div 등

문서마다의 입력베이스 클래스 나는 그것을 해시 키를 매핑하는 내 자신의 논리와 속성 이름의 검사를 우회하기 위해 @builder에 통과 모르겠어요 나는이 길을 잃을 경우 여기

class JsonTextFieldInput < SimpleForm::Inputs::Base 
    def input 
    "#{@builder.text_field(???, input_html_options)}".html_safe 
    end 
end 

그러나입니다. 또한 양식 입력 만 변경하고 레이블은 변경하지 않으며 수정해야합니다. 두 경우 모두 나는 아주 멀어 질 수 없었고 어떤 지침을 사용할 수있었습니다.

답변

3

나는 jsonb/JSON 입력이 사용하고 있습니다

class JsonbInput < SimpleForm::Inputs::StringInput 
    def input() 
    out = ActiveSupport::SafeBuffer.new 
    Hash[object.send(attribute_name).sort].each do | k, v| 
     out << template.content_tag(:div, class: 'group') do 
     template.concat @builder.label(k, object.send(attribute_name), label_html_options) 
     template.concat @builder.text_field(k, input_html_options) 
     end 
    end 
    out 
    end 

    def input_html_options 
    {class: 'string form-control'} 
    end 

end 

는 또한 모델에서 store_accessor를 사용해야합니다.

관련 문제