2014-12-05 2 views
0

사용자 지정 컬렉션을 사용하여 일정이 포함 된 확인란을 표시하고 있습니다. 저장하지만 편집하려고하면 나에게 반환됩니다. 왜?모델 수정시 활성 관리자 확인란이 선택되지 않았습니다.

f.inputs for: :schedule, name: 'Employee Schedule' do |sf| 
    sf.input :sunday, as: :check_boxes, collection: available_hours, method: :to_s 
    sf.input :monday, as: :check_boxes, collection: available_hours, method: :to_s 
    sf.input :tuesday, as: :check_boxes, collection: available_hours, method: :to_s 
    sf.input :wednesday, as: :check_boxes, collection: available_hours, method: :to_s 
    sf.input :thursday, as: :check_boxes, collection: available_hours, method: :to_s 
    sf.input :friday, as: :check_boxes, collection: available_hours, method: :to_s 
    sf.input :saturday, as: :check_boxes, collection: available_hours, method: :to_s 
end 

def available_hours 
    (0..23).map { |h| ["#{h}h às #{h.next}h", h] } 
end 

helper_method :available_hours 

답변

2

나는

내 컬렉션

def available_hours 
    Array(0..23) 
end 

그리고 내 양식이 변경됩니다 발동을받는 :member_label 매개 변수가됩니다 변경되지이 질문에 대한 해결책을 발견

수정 후 :

sf.input :sunday, as: :check_boxes, collection: available_hours, member_label: Proc.new { |h| "#{h}h às #{h.next}h" } , method: :to_s 

등 ...

1

당신은 다음과 같이 선택되어있는 체크 박스를 확인해야합니다 비슷한 ["#{h}h às #{h.next}h", h, :selected]

def available_hours(_h) 
    (0..23).map { |h| ["#{h}h às #{h.next}h", h, h == _h ? :selected : ''] } 
end 

sf.input :sunday, as: :check_boxes, collection: available_hours(sh.object.sunday), method: :to_s 

... 또는 무언가를.

0

다른 상황/필요가있을 수 있지만, 내 프로젝트 중 하나에 대해 한 것은 가능한 해결책이라고 생각합니다. ActiveAdmin 편집 양식에 사용할 수있는 사용자 지정 FormStatic 입력 클래스를 만들었습니다.

module ActiveAdmin 
    module Inputs 
    class ProductsInput < ::Formtastic::Inputs::CheckBoxesInput 
     def choice_html(choice) 

     html_options = label_html_options.merge(
      :for => choice_input_dom_id(choice), 
      :class => checked?(choice[1]) ? 'checked' : nil 
     ) 

     template.content_tag(:label, choice_label(choice), html_options) 
     end 

     def collection 
     super.sort {|a, b| a[0] <=> b[0]} 
     end 

     def choice_label(choice) 
     name, id = choice 
     product = Product.find(id) 

     name = '' 
     name << template.content_tag(:span, product.human_state_name, class: 'status_tag important') + ' ' unless product.on_sale? 
     name << product.name 

     (hidden_fields? ? 
      check_box_with_hidden_input(choice) : 
      check_box_without_hidden_input(choice)) + \ 
     template.image_tag(product.listing.thumb, width: 60).html_safe + \ 
     template.content_tag(:span, name.html_safe, class: 'choice_label') 
     end 
    end 
    end 
end 

그럼 당신은이 같은 편집 블록에서 사용할 수 있습니다 :

ActiveAdmin.register Collection do 
    form do |f| 
    f.inputs 'Products' do 
     f.input :products, as: :products 
    end 
    end 
end 

컬렉션 has_many 제품을 collection_products을 통해. 컬렉션 이미

member_label: Proc.new { |h| "#{h}h às #{h.next}h" } 
를 수집 한 후