未定义的方法` - ' for" books,children":String

时间:2014-06-24 11:26:49

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

问题 - 未定义的方法` - '表示“books,children”:String

'acts_as_taggable_on_steroids'现在还没有维持一段时间。它过去一直很好,直到最近。现在,当我尝试创建或更新标签时,收到错误消息 - >         undefined方法` - '表示“books,children”:String

                  --- Codings ----

acts_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行会导致问题!

tag.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

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

1 个答案:

答案 0 :(得分:0)

@tag_list在哪里创建?它似乎曾经是一个数组,现在它是一个字符串

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

new_tag_names = @tag_list.split(', ') - tags.map(&:name)