标签: rolify

Rolify Table Error(user.add_role:admin Unknown Key Error)

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

这是我的错误:

2.2.1 :007 > user.add_role :admin
ArgumentError: Unknown key: :optional.
Run Code Online (Sandbox Code Playgroud)

我正在用cancancan和rolify运行设计.我也在运行Koudoku gem以获得订阅支付.我怀疑这个错误可能是因为我的"订阅"表还有一个"user_id"列.我能做些什么来纠正这个问题吗?

这是我的架构.

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" …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails key devise rolify cancancan

15
推荐指数
1
解决办法
2423
查看次数

尚未创建Rolify表

在捆绑安装gem(gem'rolify')之后,我使用以下命令生成了该文件,

rails g rolify Role User
Run Code Online (Sandbox Code Playgroud)

下面的文件已创建,

invoke  active_record
create    app/models/role.rb
invoke    rspec
create      spec/models/role_spec.rb
invoke      factory_girl
create        spec/factories/roles.rb
insert    app/models/role.rb
create    db/migrate/20140425070708_rolify_create_roles
insert  app/models/user.rb
create  config/initializers/rolify.rb
Run Code Online (Sandbox Code Playgroud)

然后,我给了

rake db:migrate
Run Code Online (Sandbox Code Playgroud)

它给了我警告,

[WARN] table 'Role' doesn't exist. Did you run the migration ? Ignoring rolify config.
Run Code Online (Sandbox Code Playgroud)

表也​​没有创建.我在这里错过了什么问题.这是我的迁移文件,

 class RolifyCreateRoles < ActiveRecord::Migration
    def change
     create_table(:roles) do |t|
      t.string :name
      t.references :resource, :polymorphic => true
      t.timestamps
     end

    create_table(:users_roles, :id => false) do |t|
      t.references :user
      t.references :role
    end

    add_index(:roles, :name)
    add_index(:roles, [ :name, …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 rolify

12
推荐指数
1
解决办法
1983
查看次数

CanCanCan会在异常上抛出常规Rails错误,而不是像我指定的那样闪烁消息

我正在使用CanCanCan,Devise和Rolify.

ApplicationController看起来像这样:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :new_post

  rescue_from CanCan::AccessDenied do |exception|
    redirect_to root_url, :alert => exception.message
  end

  def new_post
    @post = Post.new
  end  

end
Run Code Online (Sandbox Code Playgroud)

routes.rb看起来像这样:

  authenticate :user, lambda { |u| u.has_role? :admin or :editor } do
    get 'newsroom', to: 'newsroom#index', as: "newsroom"
    get 'newsroom/published', to: 'newsroom#published'
    get 'newsroom/unpublished', to: 'newsroom#unpublished'    
  end

# truncated for …
Run Code Online (Sandbox Code Playgroud)

devise cancan ruby-on-rails-4 rolify cancancan

12
推荐指数
1
解决办法
696
查看次数

为什么Pundit没有像CanCanCan那样与Rolify结合?

我正在使用Devise并且对使用Pundit感兴趣,但是如果它应该与Rolify集成或者它是独立的话,就无法找到.CanCanCan与Rolify很好地配合,我喜欢角色模型.我错过了Pundit和Rolify似乎没有一起使用的主要原因吗?

devise cancan ruby-on-rails-4 rolify pundit

9
推荐指数
1
解决办法
3896
查看次数

Rails 4 + Rolify Gem:用户角色未通过UI更新

我有一个编辑视图,允许我更新特定用户的角色.我已经包含了下面的代码,但是,只是为了给你一个想法,当我取消选中页面上的框时,我的控制器中的put语句正确地发现角色关联是错误的,可以在日志中进一步查看.

但是,稍后在日志中,您可以看到角色正在重新关联回"true",我不确定为什么会出现这种情况!

如果未选中复选框,我希望从用户中删除这些角色.

在此输入图像描述

用户/ edit.html.erb:

 <fieldset>
   <%= form_for @user, :url => {action: "update"}, :html => { :class => 'user-role' } do |f| %>

     <h1 class="h1-heading">User Roles</h1>
     <p class="user-paragraph"> Check the boxes to grant different roles to <%= @user.first_name %> <%= @user.last_name %>:</p>
       <%= f.label(:admin) do %>
         <%= hidden_field_tag(:admin, 0) %>
         <%= check_box_tag(:admin, 1, @user.has_role?(:admin)) %>
         Administrator
       <% end %>

       <%= f.label(:member) do %>
         <%= hidden_field_tag(:member, 0) %>
         <%= check_box_tag(:member, 1, @user.has_role?(:member)) %>
         Member
       <% end %>
     <%= f.submit class: 'btn …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-4 rolify

7
推荐指数
1
解决办法
1118
查看次数

使用Rolify定义角色

我正在尝试用Rails 4创建一个应用程序.

我正在寻找角色管理,并希望使用Rolify,因为它支持实例级角色分配.

对于其他看同一个问题的人来说,下面有2个非常好的答案(我只能打一个,但我用了两个).查看下面的lorefnon和Cyb3rDud3答案).我还在搞清楚,但是已经使用数组(如lorefnon节目)和控制器/路由功能(如Cyb3rDud3所示)进行了迁移.

令我感到困惑的是,Rolify gem的所有文档都使用控制台来定义角色.

如何在代码中定义角色?

该板上的其他人提出了一些问题,这些问题暗示他们在db:seeds文件中定义了角色.我不想这样做,因为我想控制谁使用我的种子文件比谁可以创建角色更紧密.

你在哪里做的?

所有示例都显示它是从控制台完成的.我想定义角色的列表,然后我想给角色的权限(我想用评论员对于这部分).

我有一个用户模型.我看到的另一个宝石是角色模型.它要求您在用户模型中创建角色数组.它很明显你应该在Rolify中做到这一点 - 没有一个文件能给你这一步吗?

你在哪里定义角色?

roles ruby-on-rails user-roles rolify

6
推荐指数
2
解决办法
6450
查看次数

Rails 4 - 与Rolify合作 - 允许一组角色

我正在尝试用Rails 4创建一个应用程序.

我已经用Rolify gem定义了一系列角色.

现在,我想使用权威来允许有角色的用户做某些事情.在不止一种角色可以做某件事的情况下,我已经定义了一组角色.

在我的application_policy中,我定义了私有方法,这些方法列出了我想在权威权限中使用的角色组.

我的应用程序策略实例化用户和记录.然后我将记录定义为相关模型的名称(与该模型的策略名称相同).

我有:

class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record
  end

  def index?
    false
  end

  def show?
    scope.where(:id => record.id).exists?
  end

  def create?
    false
  end

  def new?
    create?
  end

  def update?
    false
  end

  def edit?
    update?
  end

  def destroy?
    false
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      scope
    end
  end

  private


    def …
Run Code Online (Sandbox Code Playgroud)

permissions roles ruby-on-rails rolify pundit

6
推荐指数
1
解决办法
3272
查看次数

如何避免 Pundit 策略中的 N+1 显示?/更新?/销毁?

我将 ActiveAdmin gem 与 Pundit(和 Rolify)gem 一起使用。

这就是我编写策略的方式(取自: https: //github.com/activeadmin/activeadmin/blob/master/spec/support/templates/policies/application_policy.rb):

class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record
  end

  def show?
    scope.where(id: record.id).exists?
  end

  def create?
    user.has_role?(:staff, record.company)
  end

  def update?
    scope.where(id: record.id).exists?
  end

  def destroy?
    scope.where(id: record.id).exists?
  end

  def destroy_all?
    true
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      if user.admin?
        scope.all
      else
        company_ids = Company.with_role(:staff, user).map(&:id) …
Run Code Online (Sandbox Code Playgroud)

activeadmin rolify pundit

6
推荐指数
1
解决办法
480
查看次数

Rolify remove_role 从角色表中删除?

这很奇怪。我在我的 rails 3.2 应用程序中使用 Rolify + CanCan + Devise。我的用例很简单。我希望用户一次只有一个角色,因此要更改角色,我会执行以下操作:

user.remove_role "admin"
user.add_role "associate"
Run Code Online (Sandbox Code Playgroud)

对我来说奇怪的是,当我这样做时,角色“admin”被从 Roles 表中删除。为什么会这样?我不想完全消除角色,只是消除用户的给定角色。我究竟做错了什么?

这是 SQL。注意最后一个 delete from roles 语句:

3] pry(main)> u.remove_role "sub_admin"
  Role Load (0.1ms)  SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = 2 AND "roles"."name" = 'sub_admin'
   (0.0ms)  begin transaction
   (0.3ms)  DELETE FROM "users_roles" WHERE "users_roles"."user_id" = 2 AND "users_roles"."role_id" IN (2)
   (1.9ms)  commit transaction
  User Load (0.1ms)  SELECT "users".* FROM "users" INNER JOIN "users_roles" ON "users"."id" = "users_roles"."user_id" …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails-3.2 rolify

5
推荐指数
1
解决办法
2398
查看次数

如何使用cancan和devise添加角色rolify?

我知道有很多关于这个问题的问题,但没有人真正回答我正在寻找的问题.

我正在尝试使用rolify来定义我在cancan中的角色,我能够通过rails console创建一个管理员角色并为新用户设置默认角色.

我的问题是如何将一批角色设置为不同的值并将这些角色分配给某些用户?

如果您有一个很好的教程来解释这个过程也会有所帮助,那么截屏视频会更受欢迎.

谢谢!

ruby-on-rails devise cancan rolify

5
推荐指数
1
解决办法
5058
查看次数