unset roles_mask column for cancancan gem in rails 4.2 -
i want set roles_mask
integer column set roles of user
.
here user model:
class user < activerecord::base devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable has_and_belongs_to_many :colleges has_and_belongs_to_many :groups has_and_belongs_to_many :pages has_and_belongs_to_many :partners roles = %w[admin college department news page ostad] def roles=(roles) roles = [*roles].map { |r| r.to_sym } self.roles_mask = (roles & roles).map { |r| 2**roles.index(r)}.inject(0, :+) end def roles roles.reject |r| ((roles_mask.to_i || 0) & 2**roles.index(r)).zero? end end scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**roles.index(role.to_s)} > 0 "} } def role_symboles roles.map(&:to_sym) end def has_role?(role) roles.include?(role) end end
and user_params
in user controller:
def user_params params.require(:user).permit(:name, :username, :roles, :email, :password, :password_confirmation, {:college_ids => [], :group_ids => [], :page_ids => [], :partner_ids => []}) end
and here form:
<%= form_for(@user) |f| %> .... <div class="row"> <div class="large-2 medium-2 small-12 columns"> <%= f.label t("admin.user.role") , class: "inline" %> </div> <div class="large-10 medium-10 small-12 columns"> <% role in user::roles %> <span style="width:40px"> <%= check_box_tag "user[roles][#{role}]", role, @user.roles.include?(role), {:name => "user[roles][]"}%> <%= label_tag "user_roles_#{role}", role.to_s.humanize %> </span> <% end %> <%= hidden_field_tag "user[roles][]", nil %> </div> </div> <% end %>
and have roles_mask
column in user model.
but when create user, , set columns, roles_mask
column still null
.
Comments
Post a Comment