2016-03-22 41 views
15

Próbuję skonfigurować gemify Rolify i mam problem z przypisaniem roli użytkownikowi w konsoli.Błąd tabeli zwijania (user.add_role: admin Nieznany błąd klucza)

Oto mój błąd:

2.2.1 :007 > user.add_role :admin 
ArgumentError: Unknown key: :optional. 

biegnę opracować z cancancan i rolify. Używam również klejnotu Koudoku do obsługi płatności za subskrypcję. Podejrzewam, że ten błąd mógł być spowodowany faktem, że moja tabela "subskrypcje" ma również kolumnę "user_id". Czy jest coś, co mogę zrobić, aby rozwiązać ten problem?

Oto mój schemat.

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 

Dzięki.

Odpowiedz

39

Rolify rola generator generuje wzorem z następującego kodu:

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 

Parametr :optional => true jest obsługiwana w Rails wersji 5 lub nowszej. Aby obejść ten problem, po prostu usuń tę linię ze swojego modelu ról i powinieneś być gotowy. Poniżej znajduje się ostateczny kod referencyjny:

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 
+0

To zadziałało. Dzięki @pranavpr! –

+3

Dzięki! To naprawdę dobry klejnot, ale rozczarowany, że tego typu rzeczy nie są obsługiwane, zwłaszcza biorąc pod uwagę, ile aplikacji używa wersji Rails <5 – user2490003