我有一个使用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
我的测试运行时收到的错误是 PlaylistsController中的NoMethodError #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有关,因为注释掉'has friendly_id'行并且create call再次起作用。我也尝试了相关的rake任务: 正在运行
rake friendly_id:redo_slugs MODEL=playlist
给了我同样的错误。或者,在slugs表上运行向下迁移并重新运行rails g friendly_id和向上迁移,然后运行
rake friendly_id:make_slugs MODEL=playlist
完全杀死了桌子,从那时起只需要尝试播放列表#show action就会产生同样的错误。
希望这是足够的细节。我对如何回到工作状态感到很困惑。