Rails 如何让 Devise+Cancan 只能 admin 才能注册用户。

betang · 2014年07月24日 · 最后由 dddd1919 回复于 2014年07月24日 · 1723 次阅读

取消用户自主注册,让有 admin 或者特定权限的用户去创建新用户?

你可以自定义路由,也就是 不要定义 registrations#new 的路由。这样就永远无法访问到注册页面了。 由 admin 或者特定权限的用户创建新用户这个操作就是一个普通的 curd 的操作了,自己写一个 controll 就行了。 参考:https://github.com/plataformatec/devise/wiki/How-To:-Customize-routes-to-user-registration-pages

把 sign up 入口去了

user ||= User.new # guest user (not logged in)
if user.role?(:admin)
  can :manage, :all
else
  can [:read, :update], User do |u|
    u == user
  end
end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号