First time use of cancan with roles_mask. Keep getting undefined method '&' for "1":String
In edit view.
form.html.erb
<% for role in User::ROLES %>
<%= check_box_tag "user[roles][]", role, @user.roles.include?(role) %>
<%=h role.humanize %><br />
<% end %>
<%= hidden_field_tag "user[roles][]", "" %>
<% end %>
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :firstname, :lastname, :username, :roles
ROLES = %w[admin manager employee banned]
def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
end
def roles
ROLES.reject do |r|
((roles_mask || 0) & 2**ROLES.index(r)).zero?
开发者_如何学编程 end
end
def role?(base_role)
ROLES.index(base_role.to_s) <= ROLES.index(role)
end
def is?(role)
roles.include?(role.to_s)
end
end
Are you defining roles_mask somewhere as "1"? The roles accessor is dying because the bitwise & operator is not defined for the string class. You may need to do roles_mask_var.to_i where you are calling the roles setter.
ian.
精彩评论