phlex-ruby/phlex-rails

collection_check_boxes checked value issue

Closed this issue · 2 comments

ff.collection_check_boxes(
    controller, # prd/attrs, prd/codes, prd/units
    Adm::ABILITY_ACTIONS,
    :second,
    :first,
    { checked: controller.include?(second) },
)

irb(main):001:0> Adm::ABILITY_ACTIONS
=> [["Görüntüleme", "read"], ["Oluşturma", "create"], ["Güncelleme", "update"], ["Silme", "destroy"]]

irb(main):003:0> Usr::Admin.last.ability
  Usr::Admin Load (3.3ms)  SELECT "admins".* FROM "admins" ORDER BY "admins"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> 
{"prd/attrs"=>["read", "create", "update", "destroy"],
 "prd/codes"=>["read", "create", "update", "destroy"],
 "prd/units"=>["read", "create", "update", "destroy"]}

When I try to use collection_check_boxes method for my admins ability settings, I can not set checked attribute for elements. Am I missing something, or is this a missing point?

I used bootstrap form

I haven't looked in detail at the implementation, but it seems unlikely something like Bootstrap form would work out of the box in Phlex.

Phlex::Rails needs to do a significant amount of work adapting Rails' form builders.

I'm sure it would be possible to build an adapter for it, but I expect it would be easier to move the bootstrap form to a partial and then render that partial in Phlex.

Thanks for quick response. Loving Phlex ❤️‍🔥

I seem to have resolved the issue by adding OpenStruct while iterating with fields_for.

before

 @f.fields_for(:ability) do |ff|

after

 @f.fields_for(:ability, OpenStruct.new(@admin.ability)) do |ff|

ff.collection_check_boxes(
                      ....
                      ....
                      Adm::ABILITY_ACTIONS,
                      ....