【问题标题】:acts_as_taggable_on Gem not working for Rails 5act_as_taggable_on Gem 不适用于 Rails 5
【发布时间】:2017-07-01 09:33:36
【问题描述】:

我创建了一个 rails 应用程序并添加了acts-as-taggable-on gem,运行 rake db:migrate 并将该字段添加到 Article.rb。我似乎在使用 rail 5.1 应用程序时遇到了这个错误。我不知道它是什么。

宝石文件

gem 'acts-as-taggable-on', '~> 4.0'

Article.rb

class Article < ApplicationRecord
    include TheComments::Commentable

     acts_as_taggable_on :tags

        belongs_to :user

      # Denormalization methods
      # Check the documentation for information on advanced usage
        def commentable_title
        title
      end

      def commentable_url
        ['', self.class.to_s.tableize, id].join('/')
      end

      def commentable_state
        :published.to_s
      end
    end

但是我得到了这个错误:

Running via Spring preloader in process 18395
Loading development environment (Rails 5.1.2)
2.4.0-rc1 :001 > Article
NoMethodError: undefined method `acts_as_taggable_on' for Article (call 'Article.connection' to establish a connection):Class
    from app/models/article.rb:6:in `<class:Article>'
    from app/models/article.rb:1:in `<top (required)>'
    from (irb):1
2.4.0-rc1 :002 > Article
NoMethodError: undefined method `acts_as_taggable_on' for Article (call 'Article.connection' to establish a connection):Class
    from app/models/article.rb:6:in `<class:Article>'
    from app/models/article.rb:1:in `<top (required)>'

【问题讨论】:

  • 在将 gem 添加到 Gemfile 后,您是否运行了 bundle install?您是否重新启动了字符串和应用程序?

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


【解决方案1】:

他们有没有提到的版本 5。文档说版本 4 可与 Rails 4 和 5 一起使用,这是不准确的。我将以下内容添加到我的 Gemfile 并让它工作。 GitHub 链接是我的参考点。

gem "acts-as-taggable-on", "~> 5.0"

https://github.com/mbleigh/acts-as-taggable-on/issues/866

【讨论】:

    【解决方案2】:

    问题的原因在于 gem 的版本。您使用的 gem 版本不支持 Rails 5。

    您可以通过直接从 github 中提取 gem 来解决您的错误。 为此,只需在您的 gemfile 中使用以下代码:

    gem 'acts-as-taggable-on', :git => 'https://github.com/mbleigh/acts-as-taggable-on'
    

    【讨论】:

    • 所以我更新了新的 gem 重新安装了架构,运行 rake db:migrate 并且仍然得到相同的错误。好像还是无法识别该方法。
    • 当我尝试添加我得到的 gem 时:LoadError: cannot load such file -- 作为taggable-on
    • 您是否按照github.com/mbleigh/acts-as-taggable-on#installation 遵循所有安装技术。如果是,请提供详细的错误信息。
    • 谢谢。解决了
    猜你喜欢
    • 2017-03-03
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    相关资源
    最近更新 更多