如何在ROR中添加管理员 [英] how to add admin with devise in ROR
问题描述
我正在使用devise宝石,我想创建管理员
I am using devise gem and i wanna to create admin
rails g migration add_admin_to_user
rails g migration add_admin_to_user
db
def change
add_column :users, :admin , :boolean , {default: false}
end
def user
def admin?
user=User.create(email: "info@presale.ca",password: "12345678")
user.admin=true
user
user.save
admin
end
end
在索引页
<% if current_user && current_user.admin? %>
<% end %>
在def用户中有什么问题如何解决?
theres something wrong in the def user how to fix it please?
推荐答案
有一些不太理想的东西:
There's a few things that aren't ideal:
-
您的属性
User.admin
是一个布尔值,这意味着在User.rb(model)中不添加一个方法,您将可以执行User.admin?
,它将返回true或false。
Your attribute
User.admin
is a boolean, which means without adding a method in User.rb (model), you will be able to doUser.admin?
and it will return true or false.
在您的方法中创建管理员用户不是个好主意它检查当前用户是否是管理员。最快的方式是进入导轨控制台(通过您的终端)并将用户更新为admin = true,例如。 User.last.update_attribute(:admin,true)
。
It's not a good idea to create your admin user in your method that checks if the current user is an admin. The quickest way is to go into rails console (via your terminal) and updating a User to admin = true, e.g. User.last.update_attribute(:admin, true)
.
这篇关于如何在ROR中添加管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!