首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >注册表错误(user.add_role :管理未知键错误)

注册表错误(user.add_role :管理未知键错误)
EN

Stack Overflow用户
提问于 2016-03-22 09:50:05
回答 1查看 2.5K关注 0票数 15

我正在尝试设置rolify gem,但在控制台中为用户分配角色时遇到了问题。

下面是我的错误:

代码语言:javascript
复制
2.2.1 :007 > user.add_role :admin
ArgumentError: Unknown key: :optional.

我在用cancancan和rolify运行devise。我还在运行Koudoku gem以获得订阅支付支持。我怀疑这个错误可能是由于我的“订阅”表也有一个"user_id“列造成的。我能做些什么来纠正这个问题吗?

这是我的模式。

代码语言:javascript
复制
create_table "subscriptions", force: :cascade do |t|
t.string   "stripe_id"
t.integer  "plan_id"
t.string   "last_four"
t.integer  "coupon_id"
t.string   "card_type"
t.float    "current_price"
t.integer  "user_id"
t.datetime "created_at",    null: false
t.datetime "updated_at",    null: false
end

create_table "users", force: :cascade do |t|
t.string   "email",                  default: "", null: false
t.string   "encrypted_password",     default: "", null: false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer  "sign_in_count",          default: 0,  null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string   "current_sign_in_ip"
t.string   "last_sign_in_ip"
t.datetime "created_at",                          null: false
t.datetime "updated_at",                          null: false
t.string   "first_name"
t.string   "string"
t.string   "last_name"
end

 add_index "users", ["email"], name: "index_users_on_email", unique: true
 add_index "users", ["reset_password_token"], name:    
 "index_users_on_reset_password_token", unique: true

create_table "users_roles", id: false, force: :cascade do |t|
t.integer "user_id"
t.integer "role_id"
end

add_index "users_roles", ["user_id", "role_id"], name:   "index_users_roles_on_user_id_and_role_id"

end

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-22 18:13:07

Rolify角色生成器使用以下代码生成角色模型:

代码语言:javascript
复制
class Role < ActiveRecord::Base


has_and_belongs_to_many :users, :join_table => :users_roles

  belongs_to :resource,
             :polymorphic => true,
             :optional => true

  validates :resource_type,
            :inclusion => { :in => Rolify.resource_types },
            :allow_nil => true

  scopify
end

在Rails版本5和更高版本中支持:optional => true参数。要解决这个问题,只需从你的角色模型中删除这一行,你就可以开始工作了。下面是供您参考的最终代码:

代码语言:javascript
复制
class Role < ActiveRecord::Base
  has_and_belongs_to_many :users, :join_table => :users_roles

  belongs_to :resource,
             :polymorphic => true

  validates :resource_type,
            :inclusion => { :in => Rolify.resource_types },
            :allow_nil => true

  scopify
end
票数 40
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36144877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档