Ruby on Rails,cancan和默认角色分配 [英] Ruby on rails, cancan and default role assignment

查看:75
本文介绍了Ruby on Rails,cancan和默认角色分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个小型的ruby Web服务,在其中实现了cancan授权。

I have built a small ruby webservice, in this I have implemented cancan authorization.

我关注了本教程。问题是,当他们向我的网站注册时,我无法找到分配给用户的方法,即基本角色级别。

I followed this tutorial. The problem is that, I can't find out the way to assign at the user, when they do the registration to my site, the base role level.

我发现要做一个复选框,但这不是我想要的。我的想法是直接将此任务分配到registrations_controller中,但是我没有保存角色。

I find out to do this with a checkbox, but it's not what I want. My idea was to put this assignment directly into the registrations_controller, but I failed to save the role.

我希望有人能帮助我。

谢谢。

推荐答案

这对我有用

user.rb:

  after_create :default_role

  private
  def default_role
    self.roles << Role.where(:name => 'User').first
  end

这篇关于Ruby on Rails,cancan和默认角色分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆