显示循环元素的复选框

Display checkbox from loop elements

我有一个用于创建新食谱的表格。 每个食谱可以有多个标签。 我想显示所有标签的可能性并允许用户选中或取消选中它们。 我能够很好地显示标签,但我不知道如何将它们变成复选框...... 它们必须作为数组保存在字段 all_tags 中。

= simple_form_for @recipe, html: {multipart: true} do |f|
  - if @recipe.errors.any?
    #errors
      %p
        = @recipe.errors.count
        prohibited this recipe from being saved:
      %ul
        - @recipe.errors.full_messages.each do |message|
          %li= message
  .row
    .panel-body
      = f.input :title, input_html: {class: 'form-control'}
      = f.input :description, placeholder: 'Dites nous ce que vous aimez dans cette recette ? où l\'avez-vous découverte ? avec quoi l\'accompagnée vous ? ...', input_html: {class: 'form-control'}
      = f.input :image, input_html: {class: 'form-control'}
      .tag-wrapper
        - @tags.each do |tag|
          %ul
            %li.tag.fa.fa-plus
              = tag.name

试试这个:

.tag-wrapper
  - @tags.each do |tag|
    = check_box_tag tag.name

编辑:

如果您需要将它们保存为数组,请尝试以下解决方案:

= f.input :all_tags, :as => :check_boxes, :collection => @tags