Search code examples
rubyruby-on-rails-5simple-form

Simple form - Keep radio buttons checked when edit


I have few radio buttons; the issue is that when I edit the instance, the radio buttons are not checked anymore (however they have been saved in db when I created it). I would like that the radio buttons the user previoulsy checked, to be still checked when edit.

_form.html.slim

      .cov-pick-row.w-row
        - @inspiration_images.each do |img|
          - next if img.image.path.nil?
          .w-col.w-col-2.w-col-small-4.w-col-tiny-6
            label
              INPUT[type="checkbox" name="cover[inspiration_image_ids][]" id='cover_inspiration_image_ids' value="#{img.id}" class="hidden"]
              .image-cov-pick
                = cl_image_tag(img.image.path, height: 190, class: 'img-to-pick',  data: { ix: "cover-pick" }).html_safe

Solution

  • Set checked: true for which it should be check. Ex. assume you have db column check_status where you store checked value then use checked: img.check_status.

    From your sample code as much as I understand that you are saving image id on inspiration_image_ids so use can use inspiration_image_ids.include?(img.id)