简体   繁体   English

acts_as_taggable_on和select2在Active Admin中返回奇怪的结果

[英]acts_as_taggable_on and select2 returning weird results in Active Admin

So I have been playing around with acts_as_taggable_on in active admin, and for the most part everything is working as expected. 所以我一直在主动管理员中使用acts_as_taggable_on ,并且大部分内容都按预期工作。

However, whenever I search for tags, and add an existing tag to a model, it seems to save it as the ID, rather than as the name. 但是,每当我搜索标签并将现有标签添加到模型时,它似乎将其保存为ID,而不是名称。 Creation of new tags returns the name fine, and when I go to edit the object again the tags remain tagged by the name. 创建新标签会返回正确的名称,当我再次编辑对象时,标签仍会被名称标记。 But when I try and add another tag, one that already exists in the database, it returns the name in the form, and seems to save OK, but when I go back to edit the onject again the tag shows up as an ID, rather than the name. 但是当我尝试添加另一个已经存在于数据库中的标记时,它会返回表单中的名称,并且似乎保存正常,但是当我再次编辑onject时,标记显示为ID,而不是而不是名字。

In admin/gift.rb : admin/gift.rb

controller do
  def autocomplete_gift_tags
    @tags = ActsAsTaggableOn::Tag
      .where("name LIKE ?", "#{params[:q]}%")
      .order(:name)
    respond_to do |format|
      format.json { render json: @tags , only: [:id, :name], root: false }
    end
  end
end

In tag-autocomlete.js : tag-autocomlete.js

$(document).ready(function() {
  $('.tagselect').each(function() {
    var placeholder = $(this).data('placeholder');
    var url = $(this).data('url');
    var saved = $(this).data('saved');
    $(this).select2({
        tags: true,
        placeholder: placeholder,
        minimumInputLength: 1,
        initSelection: function(element, callback) {
            saved && callback(saved);
        },
        ajax: {
            url: url,
            dataType: 'json',
            data: function(term) {
                return {
                    q: term
                };
            },
            results: function(data) {
                return {
                    results: data
                };
            }
        },
        createSearchChoice: function(term, data) {
            if ($(data).filter(function() {
                return this.name.localeCompare(term) === 0;
            }).length === 0) {
                return {
                    id: term,
                    name: term
                };
            }
        },
        formatResult: function(item, page) {
            return item.name;
        },
        formatSelection: function(item, page) {
            return item.name;
        }
    });
  });
});

And in my _gift_form.html.erb : 在我的_gift_form.html.erb

<%= f.input :tag_list, label: "Tags", input_html: { data: { placeholder: "Enter tags",  saved: f.object.tags.map{|t| {id:  t.name, name: t.name}}.to_json, url: autocomplete_gift_tags_path }, class: 'tagselect' } %>

Can't work out why the new ones are working, but the existing tags are not. 无法解决为什么新的工作正常,但现有的标签不是。

change this: 改变这个:

respond_to do |format|
  format.json { render json: @tags , only: [:id, :name], root: false }
end

to this: 对此:

respond_to do |format|
  format.json { render :json => @tags.collect{|t| {:id => t.name, :name => t.name }}}
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM