添加collection_check_boxes方法以使用ActsAsTaggableOn gem添加标签

时间:2014-07-03 15:17:00

标签: ruby-on-rails ruby checkbox acts-as-taggable-on

我正在使用ActsAsTaggableOn gem,并希望从标记列表中选择多个标记。所以我尝试使用下拉列表选择多个标签。代码如下:

    = form_for(@post) do |f|
      - if @post.errors.any?
      #error_explanation
        %h2
          = pluralize(@post.errors.count, "error")
          prohibited this post from being saved:
        %ul
          - @post.errors.full_messages.each do |msg|
            %li= msg
      .field
        = f.label :title
        %br/
        = f.text_field :title
      .field
        = f.label :body
        %br/
        = f.cktext_area :body, :class => 'ckeditor'
      .field
        = f.label :tag_list
        %br/
        = f.collection_select :tag_list, ActsAsTaggableOn::Tag.all, :id, :name, :prompt => "--- Select Tags ---", {:multiple => true}
      .actions
        = f.submit

但不知怎的,我只能选择单个标签而不是多个标签。所以我现在使用复选框来选择多个标签。代码中的修改后的行如下所示:

    = f.collection_check_boxes :tag_list, ActsAsTaggableOn::Tag.all, :id, :name

现在输出看起来像,

Tag list
[#", label="Check">, #", label="Invitation">, #", label="Payment">, #", label="Complain">, #", label="Maintenance">, #", label="Meeting">]

我不确定是什么问题。我是ruby的新手,它可能有一个简单的问题解决方案,我无法弄清楚。请帮助我,因为我已经谷歌搜索了一天并尝试了所有更改,但仍然坚持这个问题。

修改

删除raw后,它会呈现为:

Tag list
[#<struct MetaSearch::Check box="<input id=\"post_tag_list_1\" name=\"post[tag_list][]\" type=\"checkbox\" value=\"1\" />", label="<label for=\"post_tag_list_1\">Check</label>">, #<struct MetaSearch::Check box="<input id=\"post_tag_list_2\" name=\"post[tag_list][]\" type=\"checkbox\" value=\"2\" />", label="<label for=\"post_tag_list_2\">Invitation</label>">, #<struct MetaSearch::Check box="<input id=\"post_tag_list_3\" name=\"post[tag_list][]\" type=\"checkbox\" value=\"3\" />", label="<label for=\"post_tag_list_3\">Payment</label>">, #<struct MetaSearch::Check box="<input id=\"post_tag_list_4\" name=\"post[tag_list][]\" type=\"checkbox\" value=\"4\" />", label="<label for=\"post_tag_list_4\">Complain</label>">, #<struct MetaSearch::Check box="<input id=\"post_tag_list_5\" name=\"post[tag_list][]\" type=\"checkbox\" value=\"5\" />", label="<label for=\"post_tag_list_5\">Maintenance</label>">, #<struct MetaSearch::Check box="<input id=\"post_tag_list_6\" name=\"post[tag_list][]\" type=\"checkbox\" value=\"6\" />", label="<label for=\"post_tag_list_6\">Meeting</label>">] 

1 个答案:

答案 0 :(得分:0)

用于选择多个标签http://ivaynberg.github.io/select2/ - 多值选择框