如何修复由FriendlyId引起的Controller#create上的NoMethodErrod

发布于 2024-11-24 06:27:50 字数 1545 浏览 3 评论 0原文

我有一个使用FriendlyId gem 的rails 应用程序,最近我开始在开发和测试中遇到NoMethodErrors 问题。这个问题最近才开始出现,我们对项目进行了几次迭代(heroku 上的“staging”仍然有效)。

为友好 ID 设置的任何模型创建新记录时都会出现此问题。这是这些模型之一:

class Playlist < ActiveRecord::Base

  attr_accessible :title, :drill_tokens, :program, :order  , :get_playlists
  validates_presence_of :title
  validates_uniqueness_of :title

  has_many :playlist_items, :dependent => :destroy, :order => :id
  has_many :drills, :through => :playlist_items

  attr_reader :drill_tokens
  attr_reader :get_playlists

  def drill_tokens=(ids)
    self.drill_ids = ids.split(",")
  end

  has_friendly_id :title, :use_slug => true

end

我在测试运行时收到的错误是 NoMethodError in PlaylistsController#create 错误消息的正文是这样的:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

跟踪从这里开始:

friendly_id (3.2.1.1) lib/friendly_id/active_record_adapter/slug.rb:53:in `enable_name_reversion'
activesupport (3.0.7) lib/active_support/callbacks.rb:415:in `_run_save_callbacks'

我已经确认它与Friendly_id gem,注释掉“hasFriendly_id”行并且创建调用再次起作用。我也尝试过相关的 rake 任务: 运行

rake friendly_id:redo_slugs MODEL=playlist

给了我同样的错误。或者,在 slugs 表上运行向下迁移,然后重新运行rails gFriendly_id 和向上迁移,然后运行

rake friendly_id:make_slugs MODEL=playlist

完全杀死该表,从那时起,只要尝试 playlist#show 操作就会出现相同的错误。

希望这是足够的细节。我对如何回到工作状态感到非常困惑。

I have a rails app that uses the FriendlyId gem and recently I've started to get NoMethodErrors in development and test. This problem has only started recently and we're a couple of iterations into the project ('staging' which is on heroku is still working).

The problem occurs on creating a new record of any model that is setup for friendly Id's. Here's one of those models:

class Playlist < ActiveRecord::Base

  attr_accessible :title, :drill_tokens, :program, :order  , :get_playlists
  validates_presence_of :title
  validates_uniqueness_of :title

  has_many :playlist_items, :dependent => :destroy, :order => :id
  has_many :drills, :through => :playlist_items

  attr_reader :drill_tokens
  attr_reader :get_playlists

  def drill_tokens=(ids)
    self.drill_ids = ids.split(",")
  end

  has_friendly_id :title, :use_slug => true

end

The error I receive when my tests run is NoMethodError in PlaylistsController#create the body of the error message is this:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

And the trace starts here:

friendly_id (3.2.1.1) lib/friendly_id/active_record_adapter/slug.rb:53:in `enable_name_reversion'
activesupport (3.0.7) lib/active_support/callbacks.rb:415:in `_run_save_callbacks'

I've confirmed it is related to the friendly_id gem, as commenting out the 'has friendly_id' line and the create call works again. I've also tried the related rake tasks:
running

rake friendly_id:redo_slugs MODEL=playlist

gives me the same error. Alternately, running a down-migration on the slugs table and re-running rails g friendly_id and the up-migration, then running

rake friendly_id:make_slugs MODEL=playlist

totally kills the table, and from then on just trying the playlist#show action gives the same error.

Hope this is enough detail. I am thoroughly confused as to how to get back to the working state.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文