在 Rails 3 应用程序中,我正在尝试为使用 friendly_id
的模型编写种子(在 db/seeds.rb 中) :
# /db/seeds.rb
Page.create(:title => "Default page", :content => "Default content of the default page")
当我运行 rake db:seed
任务失败。以下是使用 --trace
运行时的要点:
rake aborted!
undefined local variable or method `friendly_id' for #<Class:0x007fa1de992ac8>
/Users/cornelius/.rvm/gems/ruby-1.9.2-p290@usg/gems/activerecord-3.2.5/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
/Users/cornelius/Sites/usg/app/models/page.rb:2:in `<class:Page>'
/Users/cornelius/Sites/usg/app/models/page.rb:1:in `<top (required)>'
...
这是模型:
# /app/models/page.rb
class Page < ActiveRecord::Base
extend friendly_id
friendly_id :title, use: :slugged
end
我正在使用 friendly_id
作为 Gem:
# /Gemfile
gem 'friendly_id'
有什么帮助吗?
最佳答案
在您的模型中,您需要适本地扩展类,类名是 FriendlyId,而不是 friendly_id。
作为 Ruby 和 Rails 中的约定,类名是 CamelCase,文件名是 snake_case。
您的页面模型应为:
class Page < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
end
关于ruby-on-rails - friendly_id,播种数据库,未定义方法 'friendly_id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10969341/