Search code examples
ruby-on-railsruby-on-rails-3activeadminformtastic

Active Admin checkboxes not selected when edit model


I'm using a custom collection to display checkboxes with schedule. It saves, but when I try to edit it returns to me unchecked. Why?

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

Solution

  • I found a solution for this question

    My collection remains unaltered

    def available_hours
      Array(0..23)
    end
    

    And my form will have a :member_label parameter receiving a Proc that will change it after collection already gathered

    member_label: Proc.new { |h| "#{h}h às #{h.next}h" }
    

    After modifications:

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

    and so on...