【问题标题】:acts_as_taggable undefined method 'each' erroract_as_taggable 未定义方法“每个”错误
【发布时间】:2011-03-18 16:35:13
【问题描述】:

我正在尝试使用acts_as_taggable 插件在我的ruby on rails 应用程序中包含标签功能。我附上了代码。我已经安装了插件并运行了迁移。我收到以下错误。

undefined method `each' for "value of the parameter":String

代码

location.rb - 位置表有名称、标签(这是我在表中的附加字段,我在了解插件之前添加了它:)、城市字段

class Location < ActiveRecord::Base
  belongs_to :profile
  acts_as_taggable
end

profile.rb

class Profile < ActiveRecord::Base
  has_many :locations
  acts_as_tagger
end

location_controller.rb

def create
  @location = Location.new(params[:location])
  @location.tag_list = ["tags1","tags2"]
  if @location.save
     redirect_to(@location)
  else
     redirect_to(@profile)
  end 
end

应用程序跟踪

/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:320:in `replace'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/associations.rb:1331:in `block in collection_accessor_methods'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in `block in assign_attributes'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in `assign_attributes'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/base.rb:2775:in `attributes='
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/base.rb:2473:in `initialize'
/Users/felix/rails_projects/sample_app/app/controllers/locations_controller.rb:92:in `new'
/Users/felix/rails_projects/sample_app/app/controllers/locations_controller.rb:92:in `create'

谢谢

【问题讨论】:

  • 什么时候出现上述错误?错误出现在哪里?是否有错误的堆栈跟踪?
  • 我在控制器代码中得到了它,我已经用应用程序跟踪更新了问题
  • 您能否分享一下您最终做了什么(接受的答案中的哪个选项)来解决问题?谢谢。

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


【解决方案1】:

您是否正在使用 Ruby 1.9?这个答案的其余部分以“是”开头。如果是这样,请继续阅读。

您可能偶然发现了 1.9 的行为变化。 1.9 中的字符串不再支持each(即它们不像Ruby 1.8 那样是Enumerable)。但是您可以使用each_char,这可能是您想要的。

如果这不是您的代码崩溃,那么您可以:

  • 回到 1.8.x(很明显)
  • 通过添加方法 each 来破解 String 类(混乱且可能很危险)
  • 修复导致问题的 gem 或插件。

有一篇很棒的文章here

【讨论】:

    【解决方案2】:

    我猜,因为我不确定 params 中的值是什么(也许你可以输出 params.inspect 来获取它们);我认为你在 params[:location] 中传递给 Location.new 的值是一个字符串,它需要一个键值对的哈希值。

    也许你的意思是:Location.new(:location =&gt; params[:location])

    或者:Location.new(params)

    或者也许Location.new(params[:location]) 是正确的,但它不是应有的哈希值(这通常由您的视图代码中的表单助手为您完成)。

    【讨论】:

      【解决方案3】:

      我有一个类似的问题,acts_as_taggable 抱怨未定义的每个方法,问题是我的视图 _form 中有错误的字段。我有:

      <%= f.text_field :tags %>
      

      而不是它应该是什么:

      <%= f.text_field :tag_list %>
      

      【讨论】:

        【解决方案4】:

        尝试替换@location.tag_list = ["tags1","tags2"]

        @location.tag_list = "tags1, tags2"

        你也可以像这样添加标签

        @location.tag_list.add("tag1, tag2", parse: true)

        查看this cast了解更多信息

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-12-12
          • 2013-03-07
          • 2016-05-05
          • 1970-01-01
          • 1970-01-01
          • 2013-01-25
          • 1970-01-01
          相关资源
          最近更新 更多