2012-03-08 3 views
0

나는 Freebase를 통해 얻는 항목의 해시를 가지고 있습니다. 나는 그것을 레일 form_for 안에있는 모델처럼 포함시키고 싶다. (내가 formtastic 사용하고 있습니다)레일에 루비 해시

즉,이 ... 모델 내 해시에

<%= f.input :source, :as => :select, :collection => @subject.types, :member_label => :name, :member_value => :id, :input_html => { :class => 'combobox' } %> 

하지만 작동합니다.

제 질문은 어떻게해야 외부 경로를 form_for 헬기와 함께 사용할 수 있으며 이것이 내가 취해야 할 경로인지 여부입니다. 나는 form_tag를 사용하여 다른 경로를 취할 수 있고 간단한 루비 해쉬를 순환 할 수 있다고 확신하지만 아마도 레일스 메소드를 사용하면 더 우아하고 유지 보수가 가능할 것이라고 생각할 것입니다. 나는 또한 Freebase 데이터를 저장하지 않고 ID 만 저장합니다.

간체 배열/해시 :

[{:name=>"US President", :value=>"/m/02mjmr"}, {:name=>"Election campaign", :value=>"/m/047qllg"}, {:name=>"Event", :value=>"/m/057l6lw"}] 

또는 유리 염기 검색 결과의 전체 해시 :

[{"mid"=>"/m/02mjmr", "name"=>"Barack Obama", "notable"=>{"name"=>"US President", "id"=>"/government/us_president"}, "lang"=>"en", "score"=>156.429855}, {"mid"=>"/m/047qllg", "name"=>"Barack Obama presidential campaign, 2008, General Election", "notable"=>{"name"=>"Election campaign", "id"=>"/government/election_campaign"}, "lang"=>"en", "score"=>58.741718}, {"mid"=>"/m/057l6lw", "name"=>"Barack Obama 2009 presidential inauguration", "notable"=>{"name"=>"Event", "id"=>"/event/event"}, "lang"=>"en", "score"=>57.407536}] 

답변

2

쉽게 그래서이 작업을 수행 할 수 있어야한다 :

<%= f.collection_select :freebase_id, options_from_collection_for_select(@freebase_hash, :value, :name, @object.freebase_id) 

@ object.freebase_id는 options_from_collection_for_select에 전달되어 어느 것이 (있는 경우) 셀을 지정할 수 있도록합니다 뷰가로드 될 때 처리됩니다. 이는 편집 작업이나 유효성 검사 오류 후 양식을 다시 표시 할 때 새로운 작업에 중요합니다.