0

问题——“书籍,孩子”的未定义方法`-':字符串

'acts_as_taggable_on_steroids' 已经有一段时间没有维护了。直到最近,它一直运行良好。现在,当我尝试创建或更新标签时,我收到一条错误消息 --> undefined method `-' for "books, children":String

                  --- Codings ----

act_as_taggable.rb

   1. module ActiveRecord #:nodoc:
   2. module Acts #:nodoc:
   3.  module Taggable #:nodoc:
   4.    def self.included(base)
   5.  base.extend(ClassMethods)
   6. end


   7. module ClassMethods

   8.  def acts_as_taggable
   9.   has_many :taggings, :as => :taggable, :dependent => :destroy
   10.  has_many :tags, :through => :taggings

   11. before_save :save_cached_tag_list

   12. after_create :save_tags  
   13. after_update :save_tags   

   14.  include ActiveRecord::Acts::Taggable::InstanceMethods
   15. extend ActiveRecord::Acts::Taggable::SingletonMethods

   16. alias_method_chain :reload, :tag_list

::::::::::::::::::::::::

   17. def save_tags
   18.       return unless @tag_list

   19.     new_tag_names = @tag_list - tags.map(&:name)
   20.   old_tags = tags.reject { |tag| @tag_list.include?(tag.name) }

   21. self.class.transaction do
   22.  if old_tags.any?
   23.  taggings.find(:all, :conditions => ["tag_id IN (?)", 
   24. old_tags.map(&:id)]).each(&:destroy)
   25.       taggings.reset
   26.    end

   27.  new_tag_names.each do |new_tag_name|
   28.  tags << Tag.find_or_create_by_name(new_tag_name)
   29. end

   30.  end

   31.  true
   32. end
   33.  end

注意:第 19 行导致问题!

标签.rb

    1. class Tag < ActiveRecord::Base
    2. attr_accessible :name
    3.  has_many :taggings, :dependent => :destroy
    4.  validates :name, :presence=>true
    5. validates_uniqueness_of :name
    6.  def self.find_or_create_by_name(name)
    7.  Tag.where(:name=>name).first_or_create do |tag|
    8.  tag.name = name
    9. end
    10. end

任何帮助将不胜感激。谢谢。

4

1 回答 1

0

在哪里@tag_list创建?好像以前是数组,现在是字符串

如果您想快速修复,可以将第 19 行更改为:

new_tag_names = @tag_list.split(', ') - tags.map(&:name)
于 2014-06-24T12:46:59.180 回答