Ruby On Rails:acts_id on acts_as_taggable_on,更新slug列

时间:2015-08-20 03:53:22

标签: ruby-on-rails ruby ruby-on-rails-4 acts-as-taggable-on friendly-id

我认为我已根据这些answers

正确设置了它

但是当我尝试更新现有应用中的slug列时,我的标签就是这样:

Tag.find_each(&:save)

我收到此错误:

NameError: uninitialized constant Tag

Tag不是正确的吗?

编辑:完整错误日志

  ActsAsTaggableOn::Tag Load (0.8ms)  SELECT  "tags".* FROM "tags"  ORDER BY "tags"."id" ASC LIMIT 1000
   (0.2ms)  BEGIN
   (0.4ms)  ROLLBACK
NoMethodError: undefined method `slugged' for #<ActsAsTaggableOn::Tag:0x007faafaefc458>
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activemodel-4.2.2/lib/active_model/attribute_methods.rb:433:in `method_missing'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/friendly_id-5.1.0/lib/friendly_id/slugged.rb:295:in `should_generate_new_friendly_id?'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/friendly_id-5.1.0/lib/friendly_id/slugged.rb:304:in `set_slug'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:432:in `block in make_lambda'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:164:in `call'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:164:in `block in halting'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:504:in `call'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:504:in `block in call'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:504:in `each'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:504:in `call'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:92:in `_run_callbacks'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:776:in `_run_validation_callbacks'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activemodel-4.2.2/lib/active_model/validations/callbacks.rb:113:in `run_validations!'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activemodel-4.2.2/lib/active_model/validations.rb:334:in `valid?'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/validations.rb:58:in `valid?'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/validations.rb:83:in `perform_validations'
... 20 levels...
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/console.rb:9:in `start'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require'
    from /Users/hellomello/sites/project_app/bin/rails:8:in `<top (required)>'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/hellomello/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
    from /Users/hellomello/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/hellomello/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'2.2.1 :042 > 

1 个答案:

答案 0 :(得分:0)

NameError: uninitialized constant Tag

这意味着Tag类未在环境中加载。

尝试使用名称作用域名称,例如:ActsAsTaggableOn::Tag(如您发布的链接),而不仅仅是Tag。另外,看看您是否需要require act_as_taggable_on.rb文件,以便正确加载。当课程没有正确加载时会发生此错误。

您是否将新课程添加到/lib/目录?然后,检查(或添加)application.rb文件中是否包含此类内容:

config.autoload_paths += %W(#{config.root}/lib)