그런 일이 귀하의 경우에 작동하는지 당신이 볼시겠습니까? 더 나은 해결책을 모르는 사람이라면 시간을 할애 할 것입니다.
여러분의 의견을 기다리겠습니다. 응용 프로그램/도우미/conditions_builder.rb에서
응용 프로그램에서
class ConditionsBuilder < ActionView::Helpers::FormBuilder
def multiple_select(collection, possibilities)
content = []
id = "#{object_name}[#{collection}][]"
content << @template.hidden_field_tag(id, "")
ids = Set.new(self.object.send(collection))
possibilities.each do |p, label|
uid = @template.sanitize_to_id("#{id}#{p}")
checked = ids.include?(p)
content << @template.content_tag("div",
@template.check_box_tag(id, p, checked, :id => uid) + " " +
@template.label_tag(uid, label))
end
return content.join(" ")
end
end
/도우미 application.helper 추가/:
def conditions_form(&blk)
form_for @search, :builder => ConditionsBuilder, &blk
end
을보기에 당신은 추가 (내 경우에는 내가 HAML 사용) (이 그렇게 할 수있는 가장 좋은 방법이 있는지 확실하지 않습니다) :
내 경우
- conditions_form do |f|
- f.fields_for @search.conditions do |s|
= s.label 'Name'
= s.text_field 'name_contains'
= s.multiple_select :state_equals, ['open', 'active', 'gone'].collect{|s| [s, _state(s)]}
= f.submit _("Search"), :class => 'buttons'
나는 약간의 기본값을 설정
unless params[:search]
@search.conditions.state_equals = ['open', 'active']
end
감사합니다. –