ruby-on-rails - friendly_id,播种数据库,未定义方法 'friendly_id'

标签 ruby-on-rails database ruby-on-rails-3 friendly-id

在 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/

相关文章:

mysql - Rails ActiveRecord,从连接表中选择不同的字段并返回所有模型

ruby-on-rails - rails : how to exclude post requests for a before_action in the application controller

ruby-on-rails - 你如何规划你的 Rails 应用程序?

php - 用条件更新查询?

mysql - 单个查询中的多个 MySQL 计数

ruby-on-rails - 使用设备确认页面显示用户密码

ruby-on-rails-3 - 控制台中的 Rails3 验证正常在 test_helper 中失败

ruby-on-rails - 获取 ruby 的值(value)

ruby-on-rails - ruby 动态方法传递一个值

sql - '! =' and ' <>' postgresql 中的运算符