English 中文(简体)
添加了两次标记
原标题:acts_as_taggable_on Tags added twice

我有一个 RoR 应用程序, 允许用户在收藏中标记项目 。 我使用标签 it. js Jquery 插件, 并使用 Ajax 调用来添加和删除项目管理员的标记。 我的问题是, 每个标签都添加了两次, 这样当我做时 @ item. tags. each, 所有标签都显示两次 。

<强 > 项目主计长:

  def add_tag 
    @collection = current_user.collections.find(params[:collection_id])    
    @item = @collection.items.find(params[:id])
    @item.tag_list.add(params[:tag])   
    current_user.tag(@item, :with => @item.tag_list.to_s, :on => :tags)          
    @item.save   

    render nothing: true 
  end 

  def remove_tag 
    @item = current_user.items.find_by_id(params[:id])       
    @item.tag_list.remove(params[:tag]) 
    current_user.tag(@item, :with => @item.tag_list.to_s, :on => :tags)          
    @item.save 

    render nothing: true 
  end 

处理 AJAX 标签标签时使用 Tag- it. js 标签的 < 强度 > 正义标本:

$( #item_tags ).tagit({
      onTagAdded: function(event, tag) {          
       var add_url = $( #item_tags ).attr("data-add-url");              
        $.ajax({
          url: add_url,                      
          data: {tag: tag.text().substring(0, tag.text().length-1)},                                   
        })             
      }, 
      onTagRemoved: function(event, tag) {
        var remove_url = $( #item_tags ).attr("data-remove-url"); 
        $.ajax({
          url: remove_url,  
          type:  DELETE ,                        
          data: {tag: tag.text().substring(0, tag.text().length-1)},                                  
        })
      },
      tagSource: function(search, showChoices) {
        var autocomplete_url = $( #item_tags ).attr("data-auctocomplete-url");             
        $.ajax({
          url: autocomplete_url,        
          data: {term: search.term},                              
          success: function(choices) {
            showChoices(choices);
          }
        })           
      }
});

用户添加/ 删除标签的 < 强度 > 项\\\ form 视图 :

<ul id= item_tags  class= tagit  data-remove-url="<%= remove_tag_collection_item_path %>" data-add-url="<%= add_tag_collection_item_path %>" data-auctocomplete-url="/collections/<%[email protected] %>/items/autocomplete_tag_name"> 
      <% @item.tags.each do |tag| %>   
        <li><%= tag.name %></li>            
      <% end %>                   
</ul>

我必须指出的是,必须拥有标签所有权( 由当前用户(_user), 这样 Jquery 自动完成时只能根据当前用户先前的标签而不是所有用户来完成。 我认为问题在于我必须在标签列表中添加标签, 然后将标签列表添加到用户项目标签中。 我无法找到一个方法, 因为当前用户. tag () 的方法似乎在当前用户. tag () 被调用时覆盖了上一个项目标签, 因此我必须在先前的标签中添加新标签来保护它们 。

此外,当我提交项目时, 我需要某种方式让更新方法忽略标签属性, 因为它试图将标记属性保存到项目上, 但是它们已经用 AJAX 的电话被保存了, 所以我得到这个错误 :

ActiveRecord::AssociationTypeMismatch in ItemsController#update
ActsAsTaggableOn::Tag(#82082080) expected, got String(#72294010)

提前感谢。

PS,这就是我如何获得 自动完成的 设备管理员的工作方式:

def get_autocomplete_items(parameters)
    tags = current_user.owned_tags.named_like(parameters[:term])   
end
问题回答

你是对的:

I think the problem is that I have to add the tag to the tag_list and then add the tag_list to the user item tagging. I can t find a way around this because the current_user.tag() method seems to overwrite the previous item tags when current_user.tag() is called so I have to add the new tag to the previous tags to preserve them.

.tag 方法在给定列表中覆盖现有标签。 所以, 如果您想要添加新标签, 您似乎需要将您的新标签附加到现有标签上, 然后在新列表中通过。 但是, . tag_ list. add 实际上也创建了标签 。

所以,当你这样做的时候:

  @item.tag_list.add(params[:tag])   
  current_user.tag(@item, :with => @item.tag_list.to_s, :on => :tags)          

您确实两次添加了新标签 。

当我这样做的时候:

tag_list = profile.tag_list // oops!
tag_list.add(tags)
self.tag(profile, with: tag_list, on: :tags)

我正在创建标签列表的引用, 然后又呼吁添加它。我们需要做的是:

tag_list = profile.tags.map(&:name)

要为正在标记的对象制作一系列标签名称。 然后我们可以在列表副本上添加标签, 没问题! 不再重复标签 。

很高兴我遇到你的问题,因为这个问题使我找到一个对我有用的答案。 然而,如果图书馆能提供一个很好的方法,我会更高兴。 我无法通过阅读文件找到一个好方法。

你做得很好,不过,只是一个小错误

这里 tag_list 正在获取重复的标签, 标签中有一个是“ 坚固的” 所有的 < / 坚固 >, 还有一个是“ 坚固的” 标签, 还有一个是“ 坚固的” 标签, 还有一个是“ 坚固的” 标签, 还有一个是“ 坚固的” 标签, 没有所有者 < / 坚固的标签

您的 行正在添加标签 < 坚固 >, 没有所有者

putrent_user.tag (@ item, 包括%gt; @ item.tag_list.to_s, 包括: on_gt;: tags) 行正在添加相同的标签 和所有者

后来,当您保存对象时,由于 tag_list 正在作为对象的 无人拥有 标记的引用,所以两者都得到了保存

下列守则应罚款。

  def add_tag 
    @collection = current_user.collections.find(params[:collection_id])    
    @item = @collection.items.find(params[:id])
    tag_list = @item.all_tag_list.dup # Reference to original tag_list avoided
    # Also instead of "tag_list", use "all_tag_list" method, as "tag_list" only return tags without owner
    # Link(go to last line og Tag Ownership topic) : "https://github.com/mbleigh/acts-as-taggable-on#tag-ownership"
    tag_list.add(params[:tag])   
    current_user.tag(@item, :with => tag_list.to_s, :on => :tags)          
    @item.save   

    render nothing: true 
  end 

  def remove_tag 
    @item = current_user.items.find_by_id(params[:id])
    tag_list = @item.all_tag_list.dup # Reference to original tag_list avoided      
    tag_list.remove(params[:tag]) 
    current_user.tag(@item, :with => tag_list.to_s, :on => :tags)          
    @item.save 

    render nothing: true 
  end 

< 强度 > 微度/强度 >

def add_owned_tag 
    @some_item = Item.find(params[:id])
    owned_tag_list = @some_item.all_tag_list - @some_item.tag_list
    owned_tag_list += [(params[:tag])]
    @tag_owner.tag(@some_item, :with => stringify(owned_tag_list), :on => :tags)
    @some_item.save   
end

def stringify(tag_list)
    tag_list.inject(  ) { |memo, tag| memo += (tag +  , ) }[0..-1]
end

def remove_owned_tag 
    @some_item = Item.find(params[:id])
    owned_tag_list = @some_item.all_tag_list - @some_item.tag_list
    owned_tag_list -= [(params[:tag])]
    @tag_owner.tag(@some_item, :with => stringify(owned_tag_list), :on => :tags)
    @some_item.save   
end

注意TagAdded 事件页面载荷。请参看这里的事件样本 http://aehlke.github.com/tag-it/examples.html

//-------------------------------
// Tag events
//-------------------------------
var eventTags = $( #eventTags );
eventTags.tagit({
     availableTags: sampleTags,
     onTagRemoved: function(evt, tag) {
        console.log(evt);
        alert( This tag is being removed:   + eventTags.tagit( tagLabel , tag));
    },
    onTagClicked: function(evt, tag) {
        console.log(tag);
        alert( This tag was clicked:   + eventTags.tagit( tagLabel , tag));
    }
 }).tagit( option ,  onTagAdded , function(evt, tag) {
    // Add this callbackafter we initialize the widget,
    // so that onTagAdded doesn t get called on page load.
    alert( This tag is being added:   + eventTags.tagit( tagLabel , tag));
 });




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签