友好的id,友好的保留

时间:2013-09-29 20:16:53

标签: rspec model ruby-on-rails-4 friendly-id

嗯 - 有史以来最令人困惑的验证错误,考虑到我甚至没有使用Friendly这个词我的角色模型如何:

class Role < ActiveRecord::Base
  validates :role, presence: true

  extend FriendlyId
  friendly_id :role, use: [:slugged, :history]
end

(是的,我有正确的迁移..让我们看看那些......)

class AddSlugToRoles < ActiveRecord::Migration
  def change
    add_column :roles, :slug, :string
    add_index :roles, :slug
  end
end

现在让我们创建一个工厂女孩

FactoryGirl.define do
  factory :adminRole, :class => 'Role' do
    role "Admin"
  end

  factory :userRole, :class => 'Role' do
    role "User"
  end  
end

最后让我们编写一个规范并确保我们可以创建角色:

  context "Validation checks" do
    it "validates a role object" do
      FactoryGirl.build(:adminRole).should be_valid
    end
  end

然后我们收到一条失败的消息:

  1) Role Validation checks validates a role object
     Failure/Error: FactoryGirl.build(:adminRole).should be_valid
       expected #<Role id: nil, role: "Admin", created_at: nil, updated_at: nil, slug: "admin"> to be valid, but got errors: Friendly is reserved
     # ./spec/models/role_spec.rb:14:in `block (3 levels) in <top (required)>'

对不起什么?我在哪里使用Friendly这个词?哦,没错,我不是。请帮助。

1 个答案:

答案 0 :(得分:1)

使用单词admin的转变被认为是保留的。还有很多其他的话,比如

config.reserved_words = %w(new edit index session login logout users admin stylesheets assets javascripts images)

也是保留的。