我正试图弄清楚如何使用Rolify分配角色。
我有用户,角色和组织的模型。协会是:
用户
rolify strict: true # strict means you get true only on a role that you manually add
attr_accessor :current_role
has_one :organisation, foreign_key: :owner_id
角色
has_and_belongs_to_many :users, join_table: "users_roles"
belongs_to :resource, :polymorphic => true
组织
resourcify
belongs_to :owner, class_name: 'User'
我创建了一个名为assign_roles_controller.rb的新控制器。它有:
class AssignRolesController < ApplicationController
def index
@roles = Role.all #rewrite to exclude the global roles which are for admin only
@users = User.all
# @users = Organisation.find(@current_user.organisation).user.profiles
@organisation = Organisation.first
# @organisation = Organisation.find(@current_user.organisation)
end
def create
@user = User.find(params[:id])
@role = Role(role_params)
@organisation = Organisation.first
@user.add_role, @role, @current_user.organisation
end
我的目标是作为组织所有者的用户 - 用户分配给其他用户的任何角色都将作用于第一个用户的组织。
我不是想弄清楚如何找到相关用户或组织 - 我已经很难将他们选中所有用户和第一个组织,只是为了看看我是否可以使创建操作有效。
我为assign_roles / index创建了一个索引视图:
<div class="row">
<div class="col-md-3 col-md-offset-2">
<%= select_tag "users", options_from_collection_for_select(@users, "id", "formal_name"), { multiple: true , class: "chosen-select form-control" } %>
</div>
<!-- # roles -->
<!-- u.add_role role, current_user.org -->
<div class="col-md-3 col-md-offset-1">
<%= select_tag "roles", options_from_collection_for_select(@roles, "id", "<%= text_for_role(name)%>"), :multiple => true, :class => 'chosen-select form-control' %>
</div>
</div>
任何人都可以看到我出错的地方。创建动作根本不起作用。