Guilherme
Guilherme

Reputation: 1103

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

Upvotes: 0

Views: 1660

Answers (3)

Guilherme
Guilherme

Reputation: 1103

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...

Upvotes: 2

baxang
baxang

Reputation: 3807

It could be a different situation/need, but I think what I did for one of my project is possible a solution. I created a custom FormStatic input class that can be used for an ActiveAdmin edit form.

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

Then you can use it in an edit block like this:

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

Collection has_many products through collection_products.

Upvotes: 0

nistvan
nistvan

Reputation: 2960

You need to determine which checkbox is selected like this: ["#{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

...or something similar.

Upvotes: 1

Related Questions