acts_as_taggable_on和rails autocomplete gem - 显示标记计数

时间:2022-07-26 11:06:46

I am using the acts_as_taggable_on gem and the rails jquery autocomplete gem

我正在使用acts_as_taggable_on gem和rails jquery autocomplete gem

I know how to get the tag counts via the gem

我知道如何通过gem获取标签计数

I know that in order to display something else other than the return search results for the autocomplete I need to do something like this

我知道为了显示除自动填充的返回搜索结果之外的其他内容,我需要做这样的事情

autocomplete :tag, :name, :class_name => 'ActsAsTaggableOn::Tag', :full => true

and adding 2 more keys which are

并添加另外两个键

:extra_data , :display_value

the extra data will retrieve the more data in the search (not sure it is needed here) the display_value will call a method from within the model which is searched with the autocomplete.

额外的数据将检索搜索中的更多数据(不确定这里是否需要)display_value将从模型中调用自动完成搜索的方法。

The problem:

问题:

I don't know where to put the method for the display_value which should be something like

我不知道在哪里放置display_value的方法应该是这样的

def tags_with_counts
 "#{tag.name} x #{tag.count}
end

As I don't have a tag model file to put it in (since the acts_as_taggable_on does not generate such file.

因为我没有标记模型文件来放入它(因为acts_as_taggable_on不生成这样的文件。

But even if I did have such file, from previous experiments, that method can only use data that is retrieved with the autocomplete search, and I don't have a column with tag counts.

但即使我确实有这样的文件,从以前的实验,该方法只能使用通过自动完成搜索检索的数据,并且我没有标记计数的列。

What do I do to solve this?

我该怎么做才能解决这个问题?

2 个解决方案

#1


1  

solved it by doing

这样解决了

class Tag < ActsAsTaggableOn::Tag


  def tagcount
       num = ActsAsTaggableOn::Tagging.where(:tag_id => self.id).count
       "#{Tag.find(self.id).name} x #{num}"
  end


end

and sending the "Tag" class to the rails-autocomplete call

并将“Tag”类发送到rails-autocomplete调用

#2


0  

Assuming that you include the acts_as_taggable directive in one of your models you're essentially opening it up to a series of methods and relationships belonging to the acts-as-taggable-on module. With that said if you're wanting to add a method that is in scope of your acts-as-taggable-on module then you would simply define it in the same model.

假设您在其中一个模型中包含acts_as_taggable指令,那么您基本上将其打开为属于act-as-taggable-on模块的一系列方法和关系。如果您想要添加一个在act-as-taggable-on模块范围内的方法,那么您只需在同一模型中定义它。

For example:

例如:

class Upload < ActiveRecord::Base

  acts_as_taggable

  def tags_with_counts
   "#{tag.name} x #{tag.count}"
  end

end

FYI your tags_with_counts method was missing a closing double-quote.

你的tags_with_counts方法错过了一个结束的双引号。

#1


1  

solved it by doing

这样解决了

class Tag < ActsAsTaggableOn::Tag


  def tagcount
       num = ActsAsTaggableOn::Tagging.where(:tag_id => self.id).count
       "#{Tag.find(self.id).name} x #{num}"
  end


end

and sending the "Tag" class to the rails-autocomplete call

并将“Tag”类发送到rails-autocomplete调用

#2


0  

Assuming that you include the acts_as_taggable directive in one of your models you're essentially opening it up to a series of methods and relationships belonging to the acts-as-taggable-on module. With that said if you're wanting to add a method that is in scope of your acts-as-taggable-on module then you would simply define it in the same model.

假设您在其中一个模型中包含acts_as_taggable指令,那么您基本上将其打开为属于act-as-taggable-on模块的一系列方法和关系。如果您想要添加一个在act-as-taggable-on模块范围内的方法,那么您只需在同一模型中定义它。

For example:

例如:

class Upload < ActiveRecord::Base

  acts_as_taggable

  def tags_with_counts
   "#{tag.name} x #{tag.count}"
  end

end

FYI your tags_with_counts method was missing a closing double-quote.

你的tags_with_counts方法错过了一个结束的双引号。